Friday, October 24, 2025
HomeLanguagesPHP mb_strrichr() Function

PHP mb_strrichr() Function

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;
?>


Output

, 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.";
}
?>


Output

The last occurrence of 'o' in the haystack is: orld!

Reference: https://www.php.net/manual/en/function.mb-strrichr.php

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS