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

search for in the

strrpos> <strrev
[edit] Last updated: Fri, 18 May 2012

view this page in

strripos

(PHP 5)

strriposFindet das letzte Vorkommen der gesuchten Zeichenkette in einem String, unabhängig von Groß- und Kleinschreibung

Beschreibung

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

Findet das letzte Vorkommen der gesuchten Zeichenkette in einem String. Im Gegensatz zu strrpos() beachtet strripos() die Groß- und Kleinschreibung nicht.

Parameter-Liste

haystack

Die Zeichenkette, in der gesucht werden soll.

needle

Ist needle kein String, wird der Parameter in einen Integerwert konvertiert, der dem Ordinalwert des Zeichens entspricht.

offset

Wenn angegeben, wird die Suche diese Anzahl an Buchstaben vom Anfang des Strings gezählt stoppen. Wenn der Wert negativ ist, startet die Suche stattdessen diese Anzahl an Zeichen vom Ende des Strings aus.

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.

Warnung

Diese Funktion kann sowohl das boolsche FALSE zurückliefern, als auch einen nicht-boolschen Wert, welcher zu FALSE ausgewertet wird. Weitere Informationen entnehmen Sie bitte dem Abschnitt über die boolschen 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).

Siehe auch

  • strpos() - Sucht das erste Vorkommen des Suchstrings
  • stripos() - Findet das erste Vorkommen eines Strings, unabhängig von Groß- und Kleinschreibung
  • strrchr() - Sucht das letzte Vorkommen eines Zeichens in einem String
  • substr() - Gibt einen Teil eines Strings zurück
  • stristr() - Wie strstr, aber unabhängig von Groß- bzw. Kleinschreibung
  • strstr() - Findet das erste Vorkommen eines Strings



strrpos> <strrev
[edit] Last updated: Fri, 18 May 2012
 
add a note add a note User Contributed Notes strripos
Anonymous 31-Aug-2010 01:05
Generally speaking, linear searches are from start to end, not end to start - which makes sense from a human perspective. If you need to find strings in a string backwards, reverse your haystack and needle rather than manually chopping it up.
admin at e-xxi dot net 08-Mar-2010 07:00
strripos() has very strange behaviour when you provide search position. For some reason it searches forward from the given position, instead of searching backward, that is more logical.

For example if you want to find instanse of $what, previous to the last, strripos($where, $what, $last_what_pos-1) will not wark as expected. It will return $last_what_pos again and again. And that has no sence at all.

To prevent this, I just used $prev_last_what_pos = strripos(substr($where,0,$last_what_pos), $what);
dimmav at in dot gr 25-Sep-2008 09:31
Suppose you just need a stripos function working backwards expecting that strripos does this job, you better use the following code of a custom function named strbipos:

<?php
function strbipos($haystack="", $needle="", $offset=0) {
// Search backwards in $haystack for $needle starting from $offset and return the position found or false

   
$len = strlen($haystack);
   
$pos = stripos(strrev($haystack), strrev($needle), $len - $offset - 1);
    return ( (
$pos === false) ? false : $len - strlen($needle) - $pos );
}

// Test
$body = "01234Xy7890XYz456xy90";
$str = "xY";
$len = strlen($body);
echo
"TEST POSITIVE offset VALUES IN strbipos<br>";
for (
$i = 0; $i < $len; $i++) {
    echo
"Search in [$body] for [$str] starting from offset [$i]: [" . strbipos($body, $str, $i) . "]<br>";
}
?>

Note that this function does exactly what it says and its results are different comparing to PHP 5 strripos function.
peev[dot]alexander at gmail dot com 20-Apr-2008 02: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 03: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 17-Oct-2007 05: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 01: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 01: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 10: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 10: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

 
show source | credits | sitemap | contact | advertising | mirror sites