PHP strrpos() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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

The search is case-sensitive and counting starts at 0. It returns false when there is no match.

Syntax

Call strrpos() with the values you need. Start with this line.

PHP
$pos = strrpos("Hello World Hello", "Hello");
CallWhat it does
strrpos("Hello World Hello", "Hello")Returns 12.
strrpos("photo.backup.jpg", ".")Returns 12 (the last dot).
strrpos("abc", "x")Returns false because there is no match.

For a case-insensitive search, use strripos(). To find the first occurrence, use strpos().

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

Parameters

ParameterDescription
haystackRequired. The string to search in.
needleRequired. The text to find.
offsetOptional. Where to start (positive) or stop (negative) the search.

Return Value

Return ValueDescription
intThe zero-based position of the last match.
falseReturned when the needle is not found.

PHP Examples

Example 1 — Find the Last Match

Find where the second "Hello" begins.

PHP
<?php
$text = "Hello World Hello";
$pos = strrpos($text, "Hello");

echo $pos, PHP_EOL;

How It Works

  1. The text has "Hello" at index 0 and again later.
  2. strrpos searches from the end.
  3. It finds the second "Hello" first.
  4. That one starts at index 12.

Example 2 — No Match Returns false

A missing needle gives false.

PHP
<?php
$result = strrpos("abc", "x");

var_dump($result);

How It Works

  1. Search "abc" for "x".
  2. The letter is not found.
  3. strrpos returns false.
  4. Check with !== false before using the position.

Example 3 — Get a File Extension

Use the last dot position with substr().

PHP
<?php
$file = "photo.backup.jpg";
$dot = strrpos($file, ".");

echo substr($file, $dot + 1), PHP_EOL;

How It Works

  1. The file name has two dots.
  2. strrpos finds the last dot at index 12.
  3. Add 1 to skip the dot itself.
  4. substr returns the extension: jpg.

Frequently Asked Questions

strrpos() returns the index of the last occurrence of a substring in a string, or false if it is not found.
strpos() finds the first match. strrpos() finds the last match. Both are case-sensitive.
Yes. Use strripos() for a case-insensitive search.
A negative offset tells PHP to stop searching that many characters before the end of the string, so matches near the end are skipped.
Find the last dot with strrpos() and use substr(). Often pathinfo($file, PATHINFO_EXTENSION) is simpler and safer.

Next: strspn()

strspn() returns the length of the first part of a string that contains only characters fr.

strspn() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful