Current Location: Home> Function Categories> mb_strstr

mb_strstr

Find the first occurrence of a string in another string
Name:mb_strstr
Category:Multi-byte string
Programming Language:php
One-line Description:Search for substring $needle in the string $haystack and return part of the string after $needle

Function name: mb_strstr()

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

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

Description: The mb_strstr() function is used to find the substring $needle in the string $haystack and return part of the string after $needle. This function is similar to the native strstr() function, but supports multibyte character sets.

parameter:

  • $haystack: The original string in which to look for the substring.
  • $needle: The substring to be found.
  • $before_needle (optional): If set to true, return a partial string before $needle.
  • $encoding (optional): Specify character encoding. If not specified, internal character encoding is used.

Return value:

  • If $needle is found, a partial string after $needle is returned.
  • If $needle is not found, false is returned.

Example:

 $str = "Hello, World!"; $needle = "World"; // 在$str 中查找$needle 并返回$needle 后的部分字符串$result = mb_strstr($str, $needle); echo $result; // 输出: World! // 在$str 中查找$needle 并返回$needle 之前的部分字符串$result = mb_strstr($str, $needle, true); echo $result; // 输出: Hello, // 使用指定的字符编码进行查找$str = "你好,世界!"; $needle = "世界"; $result = mb_strstr($str, $needle, false, 'UTF-8'); echo $result; // 输出: 世界!

Notes:

  • If the $encoding parameter is not specified, internal character encoding is used by default. It is recommended to set the correct character encoding before use.
  • If the substring you are looking for is an empty string, the original string is returned.
  • The mb_strstr() function is case sensitive. If you want to do a case-insensitive search, you can use the mb_stripos() function.
Similar Functions
Popular Articles