Current Location: Home> Latest Articles> Use mb_stristr to find the first substring in a string

Use mb_stristr to find the first substring in a string

gitbox 2025-05-26

When processing strings in PHP, it is often necessary to find out whether a substring exists in another string. For strings containing multibyte characters (such as Chinese, Japanese, Korean, etc.), using ordinary string functions (such as strstr or strpos ) may cause garbled code or recognition errors. At this time, you can use the multi-byte safe function mb_stristr .

1. What is mb_stristr?

mb_strstr is a function provided by PHP multibyte string extension (mbstring) to find a substring that appears for the first time in a string. It is similar to strrist , but supports multibyte encoding and uses UTF-8 by default.

The function definition is as follows:

 mb_stristr(string $haystack, string $needle, bool $before_needle = false, ?string $encoding = null): string|false
  • $haystack : The original string to search for;

  • $needle : the substring to be found;

  • $before_needle (optional): If true , return the part before the needle;

  • $encoding (optional): character encoding, default is internal encoding (usually UTF-8);

  • Return value: If found, return the remaining string (or prefix part), and if found, return false .

2. Practical Examples

Example 1: Find and return the content after the first substring appears

Suppose we want to find out if a URL contains a specific keyword and get the part that starts with that keyword:

 <?php
$url = "https://gitbox.net/docs/php/mbstring-guide";
$result = mb_stristr($url, "php");

if ($result !== false) {
    echo "Found the substring,The result is:$result";
} else {
    echo "No substring found";
}
?>

Output:

 Found the substring,The result is:php/mbstring-guide

Explanation: mb_stristr successfully found the "php" that appeared for the first time and returned the string starting from that part.

Example 2: Get the content before the substring appears

Sometimes we care more about the part before the substring, such as getting the main path:

 <?php
$url = "https://gitbox.net/docs/php/mbstring-guide";
$result = mb_stristr($url, "php", true);

if ($result !== false) {
    echo "The part before the keyword is:$result";
} else {
    echo "No substring found";
}
?>

Output:

 The part before the keyword is:https://gitbox.net/docs/

When the third parameter is true , mb_stristr returns the content before the needle.

Example 3: Processing multibyte characters (Chinese)

Suppose we deal with a sentence containing Chinese:

 <?php
$text = "Welcomegitbox.net,This is an excellentPHPResource library。";
$result = mb_stristr($text, "php");

if ($result !== false) {
    echo "Found:$result";
} else {
    echo "No matching content was found";
}
?>

Note that "PHP" here is case-insensitive search, so even "php" can match successfully.

3. What is the difference between strpos?

  • mb_strstr is case-insensitive, while mb_strpos is case-sensitive;

  • mb_strstr returns the matching substring and its subsequent parts, while mb_strpos returns the location;

  • The most important point is that mb_stristr supports multi-byte characters to avoid Chinese garbled code.

4. Summary

mb_strstr is a powerful tool for handling tasks of finding substrings in multibyte strings, especially in UTF-8 encoding environments. Its syntax is simple and functional, and is ideal for quickly locate and extracting key parts of a string.

When you are developing PHP applications, especially when dealing with Chinese content, URL paths or user input, you should give priority to using mb_stristr , which can effectively avoid the problems of character truncation and garbled code.