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 — Trouve la position de la dernière occurrence d'une chaîne dans une autre, de façon insensible à la casse
Description
Trouve la position numérique de la dernière occurrence de needle dans la chaîne de caractères haystack . Contrairement à strrpos(), strripos() est insensible à la casse.
Liste de paramètres
- haystack
-
La chaîne dans laquelle on doit chercher.
- needle
-
Notez que needle peut être une chaîne constituée d'un ou de plusieurs caractères.
- offset
-
Le paramètre offset peu être spécifié pour commencer à chercher un nombre arbitraire de caractères dans la chaîne.
Les valeurs négatives commenceront la recherche aux caractères offset à partir du début de la chaîne de caractères.
Valeurs de retour
Retourne la position numérique de la dernière occurrence de needle . Notez que la position commence à partir de 0, et non de 1.
Si needle n'est pas trouvé, la fonction retourne FALSE.
Cette fonction peut retourner FALSE, mais elle peut aussi retourner une valeur équivalent à FALSE, utilisable dans une condition if simple. Utilisez l'opérateur === pour tester la valeur de retour exacte de cette fonction.
Exemples
Exemple #1 Exemple avec strripos()
<?php
$haystack = 'ababcd';
$needle = 'aB';
$pos = strripos($haystack, $needle);
if ($pos === false) {
echo "Désolé, impossible de trouver ($needle) dans ($haystack)";
} else {
echo "Félicitations !\n";
echo "Nous avons trouvé le dernier ($needle) dans ($haystack) à la position ($pos)";
}
?>
L'exemple ci-dessus va afficher :
Félicitations ! Nous avons trouvé le dernier (aB) dans (ababcd) à la 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
