note 35618 added to function.strrpos
| From: | lee at rn2 dot php dot net | Date: | Mon, 08 Sep 2003 15:46:09 +0000 |
| Subject: | note 35618 added to function.strrpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-55851@lists.php.net to get a copy of this message | ||
Sorry, I posted the function incorrectly. I used the function as a method in a class, hence the
recursion looked like "return $this->..." Just remove the "$this-> " from
the return statement and it will work. Below is the corrected version of the function:
//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));
}
}
By the way, I believe this function to be *much* more effecient that the reverse and search solution
when dealing with larger strings, such as entire documents or templates. Sorry for the mistake in
the first posting.
----
Manual Page -- http://www.php.net/manual/en/function.strrpos.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35618
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35618&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35618&report=yes