Skip to content

Instantly share code, notes, and snippets.

@xexu
Last active August 29, 2015 13:58
Show Gist options
  • Save xexu/9972135 to your computer and use it in GitHub Desktop.
Save xexu/9972135 to your computer and use it in GitHub Desktop.
Handy StartsWith / EndsWith php functions
//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