strrchr(): Finding the Last Occurrence of a Character / sub-string in a String

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: Searching for a Substring

Although strrchr() is commonly used to find characters, it can also be used to locate substrings:
$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:
Use strrchr() to easily get file extensions from filenames.
- String Parsing: When working with log files, URLs, or file paths, strrchr() can be used to extract relevant information, such as the last directory or domain name.

Conclusion

The strrchr() function is a powerful and useful PHP string function for locating the last occurrence of a character in a string. It is ideal for tasks like parsing file paths, URLs, or extracting extensions.
String Functions Next: strrev() - Reversing a String chunk_split(): Split a string into smaller chunks
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com











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