strrchr(): Finding the Last Occurrence of a Character in a String

Find the last occurrence of a character

strrchr() returns the portion of a string beginning with the last occurrence of the requested character, or false when it is absent. If the needle contains several characters, only its first character is used. PHP 8.3 and later also support the optional before_needle argument.

$file = 'archive.backup.zip';
echo strrchr($file, '.'); // .zip

Use strrchr() for the substring from the last occurrence of a character. For the last position rather than a substring, see strrpos().

1. What is strrchr()?

The PHP strrchr() function is used to find the last occurrence of a character in a string and returns the part of the string from that character onwards.
Syntax:
strrchr(string $haystack, string $needle): string|false
- $haystack: The input string to search within.
- $needle: The character to search for.
- Return Value: It returns the portion of the string starting from the last occurrence of the needle (including the needle itself). If the needle is not found, it returns `false`.

2. Example 1: Basic Usage

The example below demonstrates how to find the last occurrence of a character in a string:
$text = "Hello world, welcome to PHP";
$result = strrchr($text, 'o');
echo $result;
Output:
o PHP
Here, the last occurrence of the character "o" is found, and the rest of the string from that point is returned.

3. Example 2: A Multi-character Needle Uses Its First Character

When the needle contains more than one character, strrchr() searches using only the first character. In this example, .com therefore searches for the final period:
$text = "Welcome to the plus2net.com programming world";
$result = strrchr($text, ".com");
echo $result;
Output:
.com programming world

4. Example 3: Handling Non-existent Characters

If the character is not found in the string, strrchr() will return `false`:
$text = "Welcome to PHP";
$result = strrchr($text, 'x');
var_dump($result);
Output:
bool(false)

5. Example 4: Extracting File Extensions

The strrchr() function is particularly useful when extracting file extensions from filenames:
$filename = "document.pdf";
$extension = strrchr($filename, '.');
echo $extension;
Output:
.pdf
This example shows how to get the file extension by searching for the last occurrence of the period . character.

6. Case Sensitivity

strrchr() is case-sensitive, meaning that searching for a lowercase character will not match its uppercase equivalent. Here's an example:
$text = "HELLO World!";
$result = strrchr($text, 'O');
echo $result;
Output:
O World!
Searching for 'O' (uppercase) would not match with lowercase o inside World since it is case-sensitive.

7. Comparing strrchr() and strchr()

While strrchr() finds the last occurrence of a character, strchr() (or its alias strstr()) finds the first occurrence of a character. Here’s a comparison: Using strrchr():
$text = "Hello world, welcome to PHP";
$result = strrchr($text, 'o');
echo $result; // Output: "o PHP"
Using strchr():
$result = strchr($text, 'o');
echo $result; // Output: "o world, welcome to PHP"

8. Best Practices for strrchr()

- Extracting File Extensions:
For a simple example, strrchr() can find a final dot; for production filename extensions prefer pathinfo($filename, PATHINFO_EXTENSION).
- String Parsing: When working with log files, URLs, or file paths, strrchr() can find a final delimiter in simple strings. Prefer purpose-built path and URL parsers for real files and URLs.

Conclusion

The strrchr() function is a powerful and useful PHP string function for locating the last occurrence of a character in a string. Use it for last-delimiter string examples; use pathinfo() and URL parsing functions when handling actual paths or URLs.
String Functions Compare: strchr() - First matching substring strrpos(): Position of the last match


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer