Last active
August 29, 2015 14:20
-
-
Save hedii/b529585bc2d8f97d4471 to your computer and use it in GitHub Desktop.
php string starts_with() ends_with()
This file contains 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
<?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