The mb_strrichr Function

The mb_strrichr function performs a case-insensitive search for the last occurrence of a substring in a string. Unlike strrchr, it works with multibyte encodings (UTF-8, etc.). Returns the part of the string from the last found occurrence to the end of the string or false if the substring is not found.

Syntax

phpmb_strrichr(
  string $haystack,
  string $needle,
  bool $before_needle = false,
  string $encoding = null
): string|false

Example

Find the last occurrence of a substring in a string case-insensitively:

php<?php
  $res = mb_strrichr('aBcAbC', 'ab');
  var_dump($res);
?>

Code execution result:

php'AbC'

Example

Find the part of the string before the found occurrence:

php<?php
  $res = mb_strrichr('aBcAbC', 'ab', true);
  var_dump($res);
?>

Code execution result:

php'aBc'

Example

Example with UTF-8 encoding specified:

php<?php
  $res = mb_strrichr('ПриветМир', 'мир', false, 'UTF-8');
  var_dump($res);
?>

Code execution result:

php'Мир'

See Also

  • the mb_strrchr function,
    which searches for the last occurrence of a substring case-sensitively
  • the mb_stripos function,
    which searches for the position of the first occurrence of a substring case-insensitively
  • the mb_strstr function,
    which searches for the first occurrence of a substring case-sensitively