Three Missing String Functions
Posted: Thu May 05, 2011 8:48 pm
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.