Skip to content

Instantly share code, notes, and snippets.

@hedii
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hedii/b529585bc2d8f97d4471 to your computer and use it in GitHub Desktop.
Save hedii/b529585bc2d8f97d4471 to your computer and use it in GitHub Desktop.
php string starts_with() ends_with()
<?php
function starts_with($haystack, $needle) {
// search backwards starting from haystack length characters from the end
return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== false;
}
function ends_with($haystack, $needle) {
// search forward starting from end minus needle length characters
return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment