PHP 8.3.4 Released!

strripos

(PHP 5, PHP 7, PHP 8)

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

Beschreibung

strripos(string $haystack, string $needle, int $offset = 0): int|false

Findet das letzte Vorkommen von needle im String haystack.

Im Gegensatz zu strrpos() beachtet strripos() die Groß- und Kleinschreibung nicht.

Parameter-Liste

haystack

Die Zeichenkette, in der gesucht werden soll.

needle

Die Zeichenkette, nach der gesucht werden soll.

Wenn der Parameter needle keine Zeichenkette ist, wird er vor PHP 8.0.0 in eine Ganzzahl umgewandelt und als ordinaler Wert eines Zeichens betrachtet. Dieses Verhalten gilt seit PHP 7.3.0 als veraltet und es wird dringend empfohlen, sich nicht darauf zu verlassen. Ist das Verhalten gewünscht, so sollte needle explizit in eine Zeichenkette umgewandelt werden oder explizit die Funktion chr() aufgerufen werden.

offset

Wenn null (0) oder positiv, wird die Suche von links nach rechts durchgeführt, wobei die ersten offset Bytes des haystack übersprungen werden.

Wenn negativ, wird die Suche von rechts nach links durchgeführt, wobei die letzten offset Bytes des haystack übersprungen werden.

Hinweis:

Dies bedeutet, dass das letzte Vorkommen von needle vor den letzten offset Bytes gesucht wird.

Rückgabewerte

Gibt die Position zurück, an der needle relativ zum Anfang der Zeichenkette haystack vorkommt (unabhängig von Suchrichtung oder Offset).

Hinweis: Positionen in Zeichenketten werden ab 0 gezählt, und nicht ab 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.

Changelog

Version Beschreibung
8.2.0 Die Groß- und Kleinschreibung hängt nicht mehr von der mit setlocale() eingestellten Locale ab. Es wird nur noch die Groß- und Kleinschreibung von ASCII-Bytes verglichen. Nicht-ASCII-Bytes werden nach ihrem Byte-Wert verglichen.
8.0.0 needle akzeptiert nun eine leere Zeichenkette.
8.0.0 Die Übergabe eines Integer als needle wird nicht mehr unterstützt.
7.3.0 Die Übergabe eines Integer als needle wird missbilligt.

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 die Position des ersten Vorkommens des Suchstrings in einem String
  • stripos() - Findet das erste Vorkommen eines Teilstrings in einem String, unabhängig von Groß- und Kleinschreibung
  • strrpos() - Findet die Position des letzten Vorkommens eines Teilstrings innerhalb einer Zeichenkette
  • strrchr() - Sucht das letzte Vorkommen eines Zeichens in einem String
  • stristr() - Wie strstr, aber unabhängig von Groß- bzw. Kleinschreibung
  • substr() - Liefert einen Teil eines Strings

add a note

User Contributed Notes 7 notes

up
4
Yanik Lupien
16 years ago
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);
}
}

?>
up
2
dimmav at in dot gr
15 years ago
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.
up
1
peev[dot]alexander at gmail dot com
16 years ago
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*/
?>
up
-1
Anonymous
13 years ago
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.
up
-1
peev[dot]alexander at gmail dot com
15 years ago
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 */
?>
up
-3
ElectroFox
16 years ago
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.
up
-7
admin at e-xxi dot net
14 years ago
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);
To Top