The mb_strrichr() function is a case-insensitive in-built function. This function searches a multi-byte string for the last occurrence of a specified character and returns the portion of the string.
Syntax:
mb_strrichr( $haystack, $needle, $before_needle , $encoding) : string|false
Parameters: This function accepts 4 parameters that are described below.
- $haystack: This parameter specifies retrieving the last occurrence of the needle from the string.
- $needle: The character or string search in $haystack.
- $before_needle: If “true”, the function returns the portion of the string that precedes $needle instead of the portion that follows it. The default is “false”.
- $encoding: This is an optional parameter with the character encoding of the string. If not provided, internal encoding is used.
Return Values: The mb_strrichr() function returns the portion of the string that follows or precedes the last occurrence of the specified character or substring, depending on the value of the $before_needle parameter. If the character or substring is not found, the function returns “false”.
Example 1: The following program demonstrates the mb_strrichr() function.
PHP
<?php $str = "Hello, world!" ; $needle = "," ; $portion = mb_strrichr( $str , $needle ); echo $portion ; ?> |
, world!
Example 2: The following program demonstrates the mb_strrichr() function.
PHP
<?php $haystack = "Hello, world!" ; $needle = "o" ; $lastOccurrence = mb_strrichr( $haystack , $needle ); if ( $lastOccurrence !== false) { echo "The last occurrence of '$needle' in the haystack is: $lastOccurrence" ; } else { echo "The needle '$needle' was not found in the haystack." ; } ?> |
The last occurrence of 'o' in the haystack is: orld!
Reference: https://www.php.net/manual/en/function.mb-strrichr.php