Page 1 of 1

Three Missing String Functions

Posted: Thu May 05, 2011 8:48 pm
by jacek
These are three functions from my standard php library. they tend to be pretty useful as I get sick of having to constantly look up the order of parameters for strpos :)
function str_starts_with($needle, $haystack){
	$needle_len = strlen($needle);
	
	return ($needle_len <= strlen($haystack) && substr($haystack, 0, $needle_len) === $needle);
}

function str_ends_with($needle, $haystack){
	$needle_len = strlen($needle);
	
	return ($needle_len <= strlen($haystack) && substr($haystack, -$needle_len) === $needle);
}

function str_contains($needle, $haystack){
	return (strpos($haystack, $needle) !== false);
}
fairly self explanatory really.

Re: Three Missing String Functions

Posted: Fri May 06, 2011 7:31 pm
by Dylan
Using a PHP library with user defined functions is a new concept to me, but I have slowly been building up one of my own.

They can really save time, and hassle and of course have the plus side of being totally user managed, therefore you control everything about it. Thanks for these functions, they will be help no doubt and definitely a sound addition to my library!