Current Location: Home> Latest Articles> How to Use mb_stristr to Check if a Substring Exists in a String? A Detailed Guide

How to Use mb_stristr to Check if a Substring Exists in a String? A Detailed Guide

M66 2025-06-26

How to Use mb_stristr to Check if a Substring Exists in a String? A Detailed Guide

In PHP, string manipulation is a common task. Whether you are dealing with user input, processing file contents, or analyzing network requests, you frequently need to search or manipulate strings. In multibyte encoding environments like UTF-8, standard string functions might not correctly handle some special characters, making mb_stristr a very practical tool.

What is mb_stristr?

mb_stristr is a function provided by PHP’s multibyte string extension (mbstring). It functions similarly to PHP’s stristr, used to search for a substring within another string, but it is specifically designed to handle multibyte character sets, such as UTF-8 or other non-ASCII encodings.

  • Functionality: Checks if the target string contains the specified substring, ignoring case.

  • Return value: If the substring exists, it returns the portion of the string from the first occurrence of the substring to the end; if the substring does not exist, it returns false.

How to Use mb_stristr?

The basic syntax of the mb_stristr function is as follows:

mb_stristr(string $haystack, string $needle, bool $before_needle = false, string $encoding = null): string|false
</span>
  • $haystack: The target string where you want to search.

  • $needle: The substring to search for.

  • $before_needle: Optional parameter. If set to true, the function returns the portion before the first occurrence of the substring. Defaults to false, which returns the portion from the first occurrence of the substring to the end.

  • $encoding: Optional parameter to specify the character encoding. By default, mb_stristr uses the internal character encoding setting.

Example: Basic Usage

Suppose you have a string and want to check if it contains a specific substring.

<?php
$haystack = "Welcome to the world of PHP programming!";
$needle = "PHP";
<p>$result = mb_stristr($haystack, $needle);</p>
<p></span>if ($result !== </span>false) {<br>
</span>echo "Substring found! Result: " . $result;<br>
} else {<br>
</span>echo "Substring not found.";<br>
}<br>
</span>?><br>
</span>

In the above code, we use mb_stristr to check if the string $haystack contains the substring $needle (which is "PHP"). If found, it returns the part of the string starting from "PHP" to the end. If not found, it returns false.

The output will be:

Substring found! Result: PHP programming world!

Case Insensitivity

Like stristr, mb_stristr is case-insensitive, meaning it ignores character case. Therefore, whether $needle is lowercase "php" or uppercase "PHP", it will still find the matching substring.

For example, the following code will also find the substring:

<?php
$haystack = "Welcome to the php programming world!";
</span>$needle = "PHP";
<p></span>$result = mb_stristr($haystack, $needle);</p>
<p></span>if ($result !== </span>false) {<br>
</span>echo "Substring found! Result: " . $result;<br>
} else {<br>
</span>echo "Substring not found.";<br>
}<br>
</span>?><br>
</span>

Using mb_stristr to Search Multibyte Characters

When working with UTF-8 strings, the standard stristr may not handle multibyte characters correctly, whereas mb_stristr avoids this problem. For example, when handling Chinese characters, mb_stristr can correctly perform substring searches.

<?php
$haystack = "This is a Chinese test string";
</span>$needle = "Chinese";
<p></span>$result = mb_stristr($haystack, $needle);</p>
<p></span>if ($result !== </span>false) {<br>
</span>echo "Substring found! Result: " . $result;<br>
} else {<br>
</span>echo "Substring not found.";<br>
}<br>
</span>?><br>
</span>

The output will be:

Substring found! Result: Chinese test string

Summary

mb_stristr is a powerful multibyte string search function, especially suitable for handling UTF-8 or other non-ASCII character sets. It is very similar to the stristr function, with the main difference being its ability to handle multibyte characters, avoiding issues caused by incompatible character sets.

By using mb_stristr correctly, you can easily detect whether a string contains a specified substring without worrying about character case or encoding. Hopefully, this guide helps you better understand how to perform string searches in PHP using mb_stristr.