note 38996 deleted from function.strpos by nlopess
| From: | nlopess@php.net | Date: | Wed, 14 Jan 2004 19:12:34 +0000 |
| Subject: | note 38996 deleted from function.strpos by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-63557@lists.php.net to get a copy of this message | ||
Note Submitter: alexandre@dutra-cancado.net
----
Here's a (very) simple function to find the last occurence of a string in another one. I
compared mine with some others which do the same job, and it seems that this one is a little bit
faster because it avoids recursivity (multiple calls to strpos()). Hope this helps!
<?php
/*
* @description: finds the last occurrence of
* $needle in $haystack,
* begining the search at the
* $start-th character in $haystack
* @returns: the start position of the
* last occurence of $needle,
* or false if $needle is not found
*/
function strlastpos($haystack, $needle, $start = 0){
$pos=strpos(
strrev(substr($haystack,$start)),
$needle===null?null:strrev($needle) //this is just to avoid errors when $needle = null (which is
unlikely :-)
);
if($pos===false) return false;
else return strlen($haystack)-$pos-strlen($needle);
}
?>