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
Syntax
strrpos(string $haystack, string $needle, int $offset = 0): int|false
Parameters
| Parameter | Description |
|---|---|
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
<?php
$path = "/var/www/site/index.php";
$slash = strrpos($path, "/");
echo substr($path, $slash + 1), "\n";
Output
index.php
<?php
echo strrpos("banana", "a"), "\n";
echo strpos("banana", "a"), "\n";
Output
5 1
Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
In other languages
str.rfind()· Pythonstring.lastIndexOf()· JavaScript
Related
strpos()· Find the position of the first occurrence of a substring. Returns false if it's not found, so compare with === false.strrchr()· Return the part of a string from the last occurrence of a character to the end. Only the first character of the needle is used.- PHP Strings · tutorial