PHP – mb_strrchr() function


The mb_strrchr() function in PHP checks the last occurrence of a character in a given string within another string. For example, suppose we have a string "ia" and we need to check it from another given string "PHP tutorials", then the mb_strrchr() function will return the portion of characters till the last existence character "ials". If it is not found, then it will return false.

Syntax

String mb_strrchr($str_haystack, $str_needle, $bool_before_needle=false, $str_encoding=empty)

Parameters

mb_strrchr() accepts the following four parameters −

  • $str_haystack − This parameter is used to get the last existence of the string needle.

  • $str_needle − This parameter is used to find the string in a haystack or from the given string.

  • $bool_before_needle − This parameter is used to decide which portion of the haystack to return. If it is set to True, then it will return all the haystack from the starting to the last existence of the needle. If it is set to False, then it will return all of the haystack from the last existence of the needle to the end.

  • $str_encoding − This parameter will return the position of the haystack or it will return False if not found.

The mb_strrchr() function returns the portion of the haystack string if the needle is found or it returns False if the needle is not found.

Example 1

mb_strrchr() PHP function using false needle

<?php
   //using string PHP Tutorials
   // using false needle
   $string=mb_strrchr("PHP Tutorials","ia", false);

   // output
   print_r($string);
?>

Output

ials

Example 2

<?php
   //using string PHP Tutorials
   // using false needle
   $string=mb_strrchr("PHP Tutorials","ia", true);

   // output
   print_r($string);
?>

Output

PHP Tutor

Example 3

<?php
   //using string PHP Tutorials
   // using false needle
   // It returns Nothing because string needle "Ia" is
   //return in capital
   $string = mb_strrchr("PHP Tutorials","Ia", false);

   // output
   print_r($string);
?>

Note − In the above example, the string needle "Ia" is in capital. Hence, it will return nothing because mb_strrchr() is case-sensitive.

Updated on: 11-Oct-2021

82 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements