The mb_strrpos Function

The mb_strrpos function finds the position of the last occurrence of a substring in a string. It works with multibyte encodings (UTF-8, etc.). Its first parameter is the string to search in, the second is the substring to find, the third (optional) is the starting position for the search, and the fourth (optional) is the encoding.

Syntax

phpmb_strrpos(
  string $haystack,
  string $needle,
  int $offset = 0,
  string $encoding
): int|false

Example

Let's find the position of the last occurrence of a substring in a string:

php<?php
  $res = mb_strrpos('аабабв', 'б');
  echo $res;
?>

Code execution result:

php3

Example

Search with UTF-8 encoding specified:

php<?php
  $res = mb_strrpos('日本語', '語', 0, 'UTF-8');
  echo $res;
?>

Code execution result:

php2

Example

When the substring is not found, the function returns false:

php<?php
  $res = mb_strrpos('abcde', 'z');
  var_dump($res);
?>

Code execution result:

phpfalse

See Also

  • the mb_strpos function,
    which finds the first occurrence of a substring
  • the strrpos function,
    which finds the last occurrence without considering encoding