Ends with
Returns true or fales depending on whether the text ends with the given string or not.
/** * EndsWith * Tests whether a text ends with the given * string or not. * * @param string * @param string * @return bool */ function EndsWith($Haystack, $Needle){ // Recommended version, using strpos return strrpos($Haystack, $Needle) === strlen($Haystack)-strlen($Needle); } // Another way, using substr function EndsWith_Old($Haystack, $Needle){ return substr($Haystack, strlen($Needle)*-1) == $Needle; }
Author:
Jonas John
License:
Public Domain
Language:
PHP
Created:
03/28/2006
Updated:
09/25/2007
Tags:
string functions, helpers
Related links:
Sorry folks, comments have been deactivated for now due to the large amount of spam.
Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!
Older comments:
function endsWith($Haystack, $Needle){
return (substr($Haystack, strlen($Needle)*(-1)) == $Needle);
}
EndsWith('//TEST//', '//');
function endsWith($h, $n){
return (false !== ($i = strrpos($h, $n)) &&
$i === strlen($h) - strlen($n));
}