PHP 8 reference · String Functions
PHP strrchr() Function
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 8 Since 4.0
Syntax
strrchr(string $haystack, string $needle, bool $before_needle = false): string|false
Parameters
| Parameter | Description |
|---|---|
haystack |
The string to search in. |
needle |
The character to find (only its first byte counts). |
before_needle optional |
true returns the part before the last match (PHP 8.3+). Default: false |
Return value
The matching part, or false.
Examples
<?php
echo strrchr("archive.tar.gz", "."), "\n";
echo strrchr("/home/user/notes.txt", "/"), "\n";
Output
.gz /notes.txt
<?php
echo strrchr("archive.tar.gz", ".", true), "\n";
Output
archive.tar
Version history
In other languages
str.rpartition()· Python
Related
strstr()· Return the part of a string from the first occurrence of a substring to the end, or the part before it.strrpos()· Find the position of the last occurrence of a substring in a string. Returns false if it isn't found.- PHP Strings · tutorial