PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

strrpos> <strrev
Last updated: Fri, 20 Jun 2008

view this page in

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

int strripos ( string $haystack , string $needle [, int $offset ] )

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.

Avertissement

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)



strrpos> <strrev
Last updated: Fri, 20 Jun 2008
 
add a note add a note User Contributed Notes
strripos
peev[dot]alexander at gmail dot com
20-Apr-2008 11:14
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 = ){
        return
strposstrtolower( $str ), strtolower( $needle ), $offset  );
    }
/* endfunction stripos */
}/* endfunction exists stripos */

if(!function_exists("strripos")){
    function
strripos$haystack, $needle, $offset = ) {
        if(  !
is_string( $needle )  )$needle = chrintval( $needle )  );
        if( 
$offset < ){
           
$temp_cut = strrevsubstr( $haystack, 0, abs($offset) )  );
        }
        else{
           
$temp_cut = strrev(    substr(   $haystack, 0, max(  ( strlen($haystack) - $offset ), )   )    );
        }
        if(   ( 
$found = stripos( $temp_cut, strrev($needle) )  ) === FALSE   )return FALSE;
       
$pos = (   strlen$haystack  ) - (  $found + $offset + strlen( $needle )  )   );
        return
$pos;
    }
/* endfunction strripos */
}/* endfunction exists strripos */
?>
peev[dot]alexander at gmail dot com
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 = strrevsubstr( $haystack, 0, abs($offset) )  );
        }
        else{
           
$temp_cut = strrevsubstr( $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*/
?>
peev[dot]alexander at gmail dot com
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 = strrevsubstr( $haystack, 0, abs($offset) )  );
        }
        else{
           
$temp_cut = strrevsubstr( $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*/
?>
ElectroFox
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.
ElectroFox
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. :)
Yanik Lupien
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);
    }
}

?>
aidan at php dot net
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

strrpos> <strrev
Last updated: Fri, 20 Jun 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites