W3docs

strripos()

The strripos() function in PHP is used to find the last occurrence of a case-insensitive substring in a string. It is similar to the strrpos() function, but is

Introduction

The strripos() function in PHP finds the position of the last occurrence of a substring inside another string, ignoring case. The name decodes as str (string) + r (reverse, i.e. search from the right) + i (case-insensitive) + pos (position).

It is the case-insensitive twin of strrpos(): strripos("Hello", "L") and strripos("Hello", "l") both return the same position, whereas strrpos() would treat L and l as different characters.

This chapter covers the syntax, the return value (and the false gotcha you must guard against), the optional $offset parameter — including negative offsets — and how strripos() relates to the other string-search functions.

Syntax

strripos(string $haystack, string $needle, int $offset = 0): int|false
ParameterDescription
$haystackThe string to search in.
$needleThe substring to search for.
$offsetOptional. Where to start the search. A positive value skips the first $offset characters; a negative value stops the search that many characters before the end of the string. Defaults to 0.

Return value: the zero-based index of the last matching occurrence, or false if $needle is not found.

Always compare with !==

strripos() returns 0 when the match is at the very start of the string, and 0 is falsy in PHP. A loose check like if (strripos(...)) would wrongly treat a position-zero match as "not found". Always compare against false with the strict operator !==:

<?php

$haystack = "Apple pie";
$pos = strripos($haystack, "a"); // matches "A" at index 0

if ($pos !== false) {
  echo "Found at index $pos"; // Found at index 0
} else {
  echo "Not found";
}

Basic example

php— editable, runs on the server

There are two o/O characters in Hello World — one in Hello (index 4) and one in World (index 7). Because strripos() reports the last match, the output is:

Found last occurrence of 'o' in 'Hello World' at position 7

Case-insensitive matching

The search treats uppercase and lowercase letters as equal, so the case of $needle does not matter:

<?php

$string = "Hello World, hello PHP";

echo strripos($string, "hello"), "\n"; // 13 — matches "hello" in "hello PHP"
echo strripos($string, "HELLO"), "\n"; // 13 — same match, case ignored

The last hello/HELLO/Hello in the string begins at index 13, regardless of how you capitalise the needle.

Using the $offset parameter

A positive offset starts the search after the first $offset characters:

<?php

$string = "Hello World";

echo strripos($string, "o", 5), "\n"; // 7 — only the "o" in "World" is considered

A negative offset makes the search stop that many characters before the end of the string, which is handy for finding the last match in everything except a trailing portion:

<?php

$string = "abcabc";

echo strripos($string, "b", -2), "\n"; // 4 — last "b" within the truncated search window

Choose the right search function for the job:

  • strrpos() — same as strripos() but case-sensitive.
  • stripos() — finds the first case-insensitive occurrence.
  • strpos() — finds the first case-sensitive occurrence.
  • strstr() — returns the rest of the string starting from the first match, rather than a position.
  • substr() — extract part of a string once you know the position.
FunctionDirectionCase-sensitive?
strpos()first matchyes
stripos()first matchno
strrpos()last matchyes
strripos()last matchno

Conclusion

strripos() locates the last case-insensitive occurrence of a substring and returns its index, or false when there is no match. Remember to test the result with !== so a position-zero match is not mistaken for a failure, and reach for $offset (positive or negative) when you need to limit the search window. When you need the first match instead, or case sensitivity matters, use one of the related functions above.

Practice

Practice
What does the PHP function strripos() do?
What does the PHP function strripos() do?
Was this page helpful?