Wednesday, July 3, 2024
HomeLanguagesPhpPHP 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

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments