Page 1 of 1

Three Missing String Functions

Posted: Thu May 05, 2011 8:48 pm
by jacek
jacek wrote:This is an archive post, any discussion should take place here http://betterphp.co.uk/board/viewtopic.php?f=5&t=8
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.