Current Location: Home> Function Categories> mb_strrchr

mb_strrchr

Find the last occurrence of a specified character in another string
Name:mb_strrchr
Category:Multi-byte string
Programming Language:php
One-line Description:Find the last occurrence of the needle in the specified string haystack and return the string and all the characters followed by it

Function name: mb_strrchr()

Applicable version: PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8

Usage: mb_strrchr(string $haystack, string $needle [, bool $part = false [, string $encoding = mb_internal_encoding()]]): string|false

Description: The mb_strrchr() function looks for the last occurrence of the needle in the specified string haystack and returns the string and all the characters after it. Unlike the standard strrchr() function, the mb_strrchr() function can handle multibyte characters.

parameter:

  • haystack: The string to look for in it.
  • needle: The character or string to be found.
  • part (optional): If set to true, return the needle and all characters followed by it. Default is false.
  • encoding (optional): character encoding. If not specified, the internal character encoding returned by mb_internal_encoding() is used.

Return value: Returns the found string, if not found, returns false.

Example:

 // 示例1:查找最后一次出现的字符$str = "Hello World"; $lastPos = mb_strrchr($str, "o"); echo $lastPos; // 输出"orld" // 示例2:查找最后一次出现的字符串$str = "Hello World"; $lastPos = mb_strrchr($str, "lo"); echo $lastPos; // 输出"lo World" // 示例3:返回needle 及其后面的所有字符$str = "Hello World"; $lastPos = mb_strrchr($str, "o", true); echo $lastPos; // 输出"o World"

Notes:

  • The mb_strrchr() function requires mbstring extension support. If the extension is not enabled, a fatal error will be thrown.
  • If the specified string haystack does not exist, the function returns false.
  • If the character encoding to be processed is different from the default internal character encoding, it is recommended to set the correct character encoding using mb_internal_encoding() before calling the function.
Similar Functions
Popular Articles