PHP | strrchr Function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Input string
$string = "Hello|welcome|to|gfg";
  
// key to be searched
$key = "|";
  
echo strrchr($string, $key);
  
?>

chevron_right


Output:

|gfg

Program 2: When $key contains escape sequence.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Input string
$string = "Hello\nwelcome\nto\ngfg";
  
// key to be searched
$key = "\n";
  
echo strrchr($string, $key);
  
?>

chevron_right


Output:


gfg

Program 3: When $key contains more than one character.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Input string
$string = "Hello|welcome|to|gfg";
  
// key to be searched
$key = "|welcome";
  
echo strrchr($string, $key);
  
?>

chevron_right


Output:

|gfg

Reference:
http://php.net/manual/en/function.strrchr.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.