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, 18 Jul 2008

view this page in

strripos

(PHP 5)

strripos — Trova la posizione dell'ultima occorrenza di una stringa in un'altra indipendentemente dalle lettere minuscole/maiusole

Descrizione

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

Restituisce la posizione numerica dell'ultima occorrenza di needle nella stringa haystack . Differentemente da strrpos(), strripos() non distingue tra lettere maiuscole minuscole. Attenzione che le posizioni della stringa partono da 0 e non da 1.

Notare anche che needle può essere una stringa di uno o più caratteri.

Se needle non è reperito, la funzione restituisce FALSE.

Avviso

Questa funzione può restituire il Booleano FALSE, ma può anche restituire un valore non-Booleano valutato come FALSE, come ad esempio 0 o "". Per favore fare riferimento alla sezione Booleans per maggiori informazioni. Usare l'operatore === per controllare il valore restituito da questa funzione.

Example #1 Un semplice esempio di strripos()

<?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)";
}
?>

Output:

   Congratulations!
   We found the last (aB) in (ababcd) at position (2)

Il parametro offset può indicare la posizione da cui cominciare la ricerca nella stringa.

Offset negativi inizieranno la ricerca alla posizione offset dall'inizio della stringa.

Vedere anche: strrpos(), strrchr(), substr(), stripos() e stristr().



strrpos> <strrev
Last updated: Fri, 18 Jul 2008
 
add a note add a note User Contributed Notes
strripos
peev[dot]alexander at gmail dot com
21-Apr-2008 05: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 07: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 08: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
03-Aug-2007 04: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
03-Aug-2007 04: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
04-Jul-2007 01: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
31-May-2004 01: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, 18 Jul 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites