Support PHP Version: PHP 7.1, PHP 7.2, PHP 7.3, PHP 7.4, PHP 8.0, PHP 8.1, PHP 8.2, PHP 8.3 With Latest All Version Support.
Hello Friends Today, through this tutorial, I will tell you How to Use `strrchr()` function using PHP, PHP 8, PHP 8.1, PHP 8.2 With Example. In PHP, the `strrchr()` function is used to find the last occurrence of a character in a string and return the portion of the string starting from that character until the end of the string. The function syntax is as follows:
<?php strrchr(string $haystack, string $needle): string|false ?>
1. `$haystack`: The string to search in.
2. `$needle`: The character or substring to search for.
Here’s an example demonstrating the usage of `strrchr()` function in PHP 8.2:
<?php $string = "Hello, world! This is a test string."; $character = 'o'; $last_occurrence = strrchr($string, $character); if ($last_occurrence !== false) { echo "Last occurrence of '$character' found: $last_occurrence"; } else { echo "Character '$character' not found in the string."; } ?>
In this example:
1. The `$string` variable contains the input string.
2. We are searching for the last occurrence of the character `’o’`.
3. The `strrchr()` function returns the substring starting from the last occurrence of `’o’` till the end of the string.
4. If the character is found, it will output the substring starting from the last occurrence of `’o’`. Otherwise, it will indicate that the character was not found in the string.
Remember, `strrchr()` is case-sensitive. If you want a case-insensitive search, you should use `strripos()` or `mb_strripos()` functions.