Forum Moderators: coopster

Message Too Old, No Replies

Find Last occurrence of a string in php 4

         

compose

11:53 am on Oct 26, 2005 (gmt 0)

10+ Year Member



Hi ,

Can any body tell how i can trape last occurance of a string in a string. I use strripos()function to do this. But it is available in php 5 ont in 4. And there is php4 installed on my web server. So my scriptting is not running properly. Can any body tell substitute of this function in php4.

Vineet.

mcibor

1:04 pm on Oct 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I found in Manual [php.net]

The "find-last-occurrence-of-a-string" functions suggested here do not allow for a starting offset, so here's one, tried and tested, that does:

function my_strrpos($haystack, $needle, $offset=0) {
// same as strrpos, except $needle can be a string
$strrpos = false;
if (is_string($haystack) && is_string($needle) && is_numeric($offset)) {
$strlen = strlen($haystack);
$strpos = strpos(strrev(substr($haystack, $offset)), strrev($needle));
if (is_numeric($strpos)) {
$strrpos = $strlen - $strpos - strlen($needle);
}
}
return $strrpos;
}

Hope this helps
Michal Cibor