note 35618 deleted from function.strrpos by vincent

From: Date: Mon, 08 Sep 2003 15:48:56 +0000
Subject: note 35618 deleted from function.strrpos by vincent
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-55853@lists.php.net to get a copy of this message
Note Submitter: lee at 5ss dot net ---- 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.

« previous php.notes (#55853) next »