Thursday, September 4, 2025
HomeLanguagesPHP mb_strrchr() Function

PHP mb_strrchr() Function

The mb_strrchr() is an inbuilt function in PHP that searches a multi-byte string for the last occurrence of a specified character and returns the portion of the string that follows it.

Syntax:

mb_strrchr( $haystack, $needle, $before_needle , $encoding) : string|false

Parameters: This function takes 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. The character encoding of the string. If not provided, internal encoding is used.

Return Value: The mb_strrchr() 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 example demonstrates the mb_strrchr() function.

PHP




<?php
$str = "Hello, world!";
$needle = ",";
$portion = mb_strrchr($str, $needle);
echo $portion;
?>


Output:

, world!

Example 2: The following example demonstrates the mb_strrchr() function.

PHP




<?php
$str = "Geeks for Geeks";
$needle = "o";
$portion = mb_strrchr($str, $needle,true);
echo $portion;
?>


Output:

Geeks f

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

RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS