OK, I guess this will be the final function implementation for PHP 4.x versions ( my previous posts are invalid )
<?php
if(!function_exists("stripos")){
function stripos( $str, $needle, $offset = 0 ){
return strpos( strtolower( $str ), strtolower( $needle ), $offset );
}/* endfunction stripos */
}/* endfunction exists stripos */
if(!function_exists("strripos")){
function strripos( $haystack, $needle, $offset = 0 ) {
if( !is_string( $needle ) )$needle = chr( intval( $needle ) );
if( $offset < 0 ){
$temp_cut = strrev( substr( $haystack, 0, abs($offset) ) );
}
else{
$temp_cut = strrev( substr( $haystack, 0, max( ( strlen($haystack) - $offset ), 0 ) ) );
}
if( ( $found = stripos( $temp_cut, strrev($needle) ) ) === FALSE )return FALSE;
$pos = ( strlen( $haystack ) - ( $found + $offset + strlen( $needle ) ) );
return $pos;
}/* endfunction strripos */
}/* endfunction exists strripos */
?>
strripos
(PHP 5)
strripos — Findet das letzte Vorkommen der gesuchten Zeichenkette in einem String, unabhängig von Groß- und Kleinschreibung
Beschreibung
Findet das letzte Vorkommen der gesuchten Zeichenkette in einem String, unabhängig von Groß- und Kleinschreibung. Im Gegensatz zu strrpos() beachtet strripos() die Groß- und Kleinschreibung nicht.
Parameter-Liste
- haystack
-
Die Zeichenkette, in der gesucht werden soll.
- needle
-
Beachten Sie, dass needle eine Zeichenkette aus einem oder mehreren Zeichen sein kann.
- offset
-
Der Parameter offset kann angegeben werden, wenn eine beliebige Anzahl Zeichen in einem String gesucht werden soll.
Negative Offset-Werte beginnen die Suche bei dem in offset angegebenen Zeichen ab Beginn des Strings.
Rückgabewerte
Gibt die numerische Position des letzten Vorkommens von needle zurück. Beachten Sie außerdem, dass die Funktion bei 0 zu zählen beginnt, nicht bei 1.
Wenn needle nicht gefunden wird, gibt die Funktion FALSE zurück.
Diese Funktion kann sowohl das Boolsche FALSE zurückliefern, als auch einen nicht-Boolschen Wert, wie zum Beispiel0 oder "". der von einem einfachen if-Statement als FALSE ausgewertet wird. Weitere Informationen entnehmen Sie bitte dem Abschnitt über die Boolsche Typen. Benutzen Sie deshalb den === Operator um den Rückgabewert dieser Funktion zu überprüfen.
Beispiele
Beispiel #1 Ein einfaches strripos()-Beispiel
<?php
$haystack = 'ababcd';
$needle = 'aB';
$pos = strripos($haystack, $needle);
if ($pos === false) {
echo "Leider wurde ($needle) nicht in ($haystack) gefunden.";
} else {
echo "Glückwunsch!\n";
echo "Das letzte Vorkommen von ($needle) in ($haystack) ist an Position ($pos).";
}
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
Glückwunsch! Das letzte Vorkommen von (aB) in (ababcd) ist an Position (2).
strripos
20-Apr-2008 11:14
23-Nov-2007 12:05
Oops, I forgot to return "false" if the needle is not found. Here is the proper function.
<?php
if(!function_exists("strripos")){
function strripos($haystack, $needle, $offset=0) {
if($offset<0){
$temp_cut = strrev( substr( $haystack, 0, abs($offset) ) );
}
else{
$temp_cut = strrev( substr( $haystack, $offset ) );
}
$pos = strlen($haystack) - (strpos($temp_cut, strrev($needle)) + $offset + strlen($needle));
if ($pos == strlen($haystack)) { $pos = 0; }
if(strpos($temp_cut, strrev($needle))===false){
return false;
}
else return $pos;
}/* endfunction strripos*/
}/* endfunction exists strripos*/
?>
18-Oct-2007 02:23
I think you shouldn't underestimate the length of $needle in the search of THE FIRST POSITION of it's last occurrence in the string. I improved the posted function, with added support for offset. I think this is an exact copy of the real function:
<?php
if(!function_exists("strripos")){
function strripos($haystack, $needle, $offset=0) {
if($offset<0){
$temp_cut = strrev( substr( $haystack, 0, abs($offset) ) );
}
else{
$temp_cut = strrev( substr( $haystack, $offset ) );
}
$pos = strlen($haystack) - (strpos($temp_cut, strrev($needle)) + $offset + strlen($needle));
if ($pos == strlen($haystack)) { $pos = 0; }
return $pos;
}/* endfunction strripos*/
}/* endfunction exists strripos*/
?>
02-Aug-2007 10:59
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.
02-Aug-2007 10: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. :)
03-Jul-2007 07: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);
}
}
?>
30-May-2004 07: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
