Three Missing String Functions

Code that the staff think is particularly good will be copied here.
Locked
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Three Missing String Functions

Post by jacek »

jacek wrote:This is an archive post, any discussion should take place here 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 :)

[syntax=php]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);
}[/syntax]

fairly self explanatory really.
Image
Locked