Last active
August 29, 2015 13:58
-
-
Save xexu/9972135 to your computer and use it in GitHub Desktop.
Handy StartsWith / EndsWith php functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://stackoverflow.com/questions/834303/php-startswith-and-endswith-functions/834355#834355 | |
//Note: Be careful with empty strings, you might want a different behaviour | |
function startsWith($haystack, $needle) | |
{ | |
$length = strlen($needle); | |
return (substr($haystack, 0, $length) === $needle); | |
} | |
function endsWith($haystack, $needle) | |
{ | |
$length = strlen($needle); | |
if ($length == 0) { | |
return true; | |
} | |
return (substr($haystack, -$length) === $needle); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment