Code that the staff think is particularly good will be copied here.
jacek
Site Admin
Posts: 3262 Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:
Post
by jacek » 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.