PHP strrpos() Function

Quick summary: The PHP strrpos() function finds the position of the last occurrence of a substring within a string.

PHP strrpos() Syntax

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

PHP strrpos() Basic examples

$text = "Hello world, world";
$pos = strrpos($text, "world");
var_dump($pos);
PHP
Output:
int(13)

Finds the position of the last occurrence of the substring "world".

$value = "abcabc";
$pos = strrpos($value, "a");
var_dump($pos);
PHP
Output:
int(3)

Returns the position of the last occurrence of the character.

$result = strrpos("example", "z");
var_dump($result);
PHP
Output:
bool(false)

If the substring is not found, strrpos() returns false.

PHP strrpos() Real-world usage

$filename = "archive.tar.gz";
$pos = strrpos($filename, ".");
echo substr($filename, $pos + 1);
PHP
Output:
gz

Extracts the file extension by locating the last dot.

$url = "https://example.com/path/to/page";
$pos = strrpos($url, "/");
echo substr($url, $pos + 1);
PHP
Output:
page

Gets the last segment of a URL path.

PHP strrpos() Edge cases

$result = strrpos("", "test");
var_dump($result);
PHP
Output:
bool(false)

Searching in an empty string always returns false.

$result = strrpos("hello", "");
var_dump($result);
PHP
Output:
int(5)

An empty substring is considered found at the end of the string.

PHP strrpos() Common mistakes

Using loose comparison with the return value

A return value of 0 or other valid positions may be incorrectly treated as false.

Incorrect
if (strrpos("abc", "a")) {
    echo "Found";
}
Correct
if (strrpos("abc", "a") !== false) {
    echo "Found";
}

Always use strict comparison (!== false) when checking strrpos().

Confusing strpos with strrpos

strpos() finds the first occurrence, while strrpos() finds the last.

Incorrect
$pos = strpos("one two one", "one");
var_dump($pos);
Correct
$pos = strrpos("one two one", "one");
var_dump($pos);

Use strrpos() when you need the last occurrence.

PHP strrpos() Frequently Asked Questions

What does strrpos() do in PHP?

strrpos() finds the position of the last occurrence of a substring in a string.

What does strrpos() return if the substring is not found?

strrpos() returns false if the substring is not found in the string.

Why is strrpos() returning 0?

strrpos() returns 0 when the substring is found at the beginning of the string. This is a valid result and not an error.

How to correctly check if strrpos() found a value?

Use strict comparison: if (strrpos($string, $search) !== false) to correctly detect matches.

Is strrpos() case-sensitive?

Yes, strrpos() is case-sensitive. Use strripos() for case-insensitive searches.

What is the difference between strrpos() and strpos()?

strpos() finds the first occurrence of a substring, while strrpos() finds the last occurrence.

Can strrpos() search from a specific position?

Yes, you can provide an offset as the third argument to control where the search starts.

Does strrpos() work with multibyte strings?

No, strrpos() is not multibyte-safe. Use mb_strrpos() for UTF-8 or multibyte strings.

What is the difference between strrpos() and strripos()?

strrpos() is case-sensitive, while strripos() performs a case-insensitive search.

Can strrpos() return negative values?

No, strrpos() returns either a zero-based position or false. It never returns negative values.

How to find the last occurrence of a character in PHP?

Use strrpos($string, $char) to find the last position of a character in a string.

What is a real-world use case of strrpos()?

strrpos() is commonly used to extract file extensions, parse paths, or find the last occurrence of delimiters in strings.

PHP strrpos() Related PHP Functions