PHP 8 reference · String Functions

PHP strrpos() Function

Find the position of the last occurrence of a substring in a string. Returns false if it isn't found.

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

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

Parameters

ParameterDescription
haystack The string to search in.
needle The string to find.
offset optional Where to start; a negative value stops the search that many bytes from the end. Default: 0

Return value

The position of the last match, or false.

Examples

Text after the last slashTry it yourself
<?php
$path = "/var/www/site/index.php";
$slash = strrpos($path, "/");
echo substr($path, $slash + 1), "\n";

Output

index.php
Last vs firstTry it yourself
<?php
echo strrpos("banana", "a"), "\n";
echo strpos("banana", "a"), "\n";

Output

5
1

Version history

VersionChange
PHP 4.0Added

In other languages

Was this page helpful?