note 35387 modified in function.strrpos by vincent
| From: | vincent@php.net | Date: | Mon, 08 Sep 2003 15:48:46 +0000 |
| Subject: | note 35387 modified in function.strrpos by vincent | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-55852@lists.php.net to get a copy of this message | ||
I should have looked here first, but instead I wrote my own version of strrpos that supports
searching for entire strings, rather than individual characters. This is a recursive function. I
have not tested to see if it is more or less efficient than the others on the page. I hope this
helps someone!
//Find last occurance of needle in haystack
function str_rpos($haystack, $needle, $start = 0){
$tempPos = strpos($haystack, $needle, $start);
if($tempPos === false){
if($start == 0){
//Needle not in string at all
return false;
}else{
//No more occurances found
return $start - strlen($needle);
}
}else{
//Find the next occurance
return str_rpos($haystack, $needle, $tempPos + strlen($needle));
}
}
--was--
I should have looked here first, but instead I wrote my own version of strrpos that supports
searching for entire strings, rather than individual characters. This is a recursive function. I
have not tested to see if it is more or less efficient than the others on the page. I hope this
helps someone!
//Find last occurance of needle in haystack
function str_rpos($haystack, $needle, $start = 0){
$tempPos = strpos($haystack, $needle, $start);
if($tempPos === false){
if($start == 0){
//Needle not in string at all
return false;
}else{
//No more occurances found
return $start - strlen($needle);
}
}else{
//Find the next occurance
return $this->str_rpos($haystack, $needle, $tempPos + strlen($needle));
}
}
http://www.php.net/manual/en/function.strrpos.php