The strrchr() function is a built-in function in PHP. This function takes two arguments a string and a character. This function searches the given character in the given string and returns the portion of string starting from the last occurrence of the given character in that string.
Syntax:
strrchr($string, $key)
Parameters: This function accepts two parameters. Both of the parameters are mandatory and are described below:
- $string: This is the input string in which we want to search the given key.
- $key: This parameter represents a character to be searched in the given string $string. If this parameter contains more than one character then only the first character of this parameter will be searched in $string.
Return Value: This function returns the portion of $string starting from the last occurrence of the given $key in that string.
Examples:
Input : $string = "Hello|welcome|to|gfg" $key = '|' Output : |gfg Input : $string = "Welcome\nto\ngfg" $key = '\n' Output : gfg
Below programs illustrate the strrchr() function in PHP:
Program 1:
<?php // Input string$string = "Hello|welcome|to|gfg"; // key to be searched$key = "|"; echo strrchr($string, $key); ?> |
Output:
|gfg
Program 2: When $key contains escape sequence.
<?php // Input string$string = "Hello\nwelcome\nto\ngfg"; // key to be searched$key = "\n"; echo strrchr($string, $key); ?> |
Output:
gfg
Program 3: When $key contains more than one character.
<?php // Input string$string = "Hello|welcome|to|gfg"; // key to be searched$key = "|welcome"; echo strrchr($string, $key); ?> |
Output:
|gfg


