Sorry, I made that last post a bit prematurely. One more thing wrong with the simple php4 version is that it breaks if the string is not found. It should actually look like this:
<?php
if (function_exists('strripos') == false) {
function strripos($haystack, $needle) {
$pos = strlen($haystack) - strpos(strrev($haystack), strrev($needle));
if ($pos == strlen($haystack)) { $pos = 0; }
return $pos;
}
}
?>
Note, we now check to see if the $needle was found, and if it isn't, we return 0.
strripos
(PHP 5)
strripos — Find position of last occurrence of a case-insensitive string in a string
Description
int strripos ( string $haystack, string $needle [, int $offset] )Find position of last occurrence of a case-insensitive string in a string. Unlike strrpos(), strripos() is case-insensitive.
Parameters
- haystack
- needle
Note that the needle may be a string of one or more characters.
- offset
The offset parameter may be specified to begin searching an arbitrary number of characters into the string.
Negative offset values will start the search at offset characters from the start of the string.
Return Values
Returns the numerical position of the last occurence of needle. Also note that string positions start at 0, and not 1.
If needle is not found, FALSE is returned.
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
Examples
Example 2441. A simple strripos() example
<?php
$haystack = 'ababcd';
$needle = 'aB';
$pos = strripos($haystack, $needle);
if ($pos === false) {
echo "Sorry, we did not find ($needle) in ($haystack)";
} else {
echo "Congratulations!\n";
echo "We found the last ($needle) in ($haystack) at position ($pos)";
}
?>
The above example will output:
Congratulations!
We found the last (aB) in (ababcd) at position (2)
See Also
| strpos() |
| stripos() |
| strrchr() |
| substr() |
| stristr() |
| strstr() |
strripos
03-Aug-2007 05:59
03-Aug-2007 05:39
Actually, the above, "Simple way to implement this function in PHP 4" by Yanik Lupien, should be:
<?php
if (function_exists('strripos') == false) {
function strripos($haystack, $needle) {
return strlen($haystack) - strpos(strrev($haystack), strrev($needle));
}
}
?>
Note the reversal (<?php strrev($needle)?>) of the search string. This was left out in Yanik's example, and without it, you'll simply get the length of the haystack, as the forward string will not likely be found in the reversed haystack.
Thus; if we reverse the haystack, any instance of the search string ($needle) therein will also be reversed, so we must reverse it to look for it. :)
04-Jul-2007 02:47
Simple way to implement this function in PHP 4
<?php
if (function_exists('strripos') == false) {
function strripos($haystack, $needle) {
return strlen($haystack) - strpos(strrev($haystack), $needle);
}
}
?>
31-May-2004 02:36
This functionality is now implemented in the PEAR package PHP_Compat.
More information about using this function without upgrading your version of PHP can be found on the below link:
http://pear.php.net/package/PHP_Compat