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

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

Syntax

strrchr(string $haystack, string $needle, bool $before_needle = false): string|false

Parameters

ParameterDescription
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

From the last matchTry it yourself
<?php
echo strrchr("archive.tar.gz", "."), "\n";
echo strrchr("/home/user/notes.txt", "/"), "\n";

Output

.gz
/notes.txt
Before the last matchTry it yourself
<?php
echo strrchr("archive.tar.gz", ".", true), "\n";

Output

archive.tar

Version history

VersionChange
PHP 4.0Added
PHP 8.3Added the before_needle parameter.

In other languages

Was this page helpful?