Current Location: Home> Latest Articles> Will there be problems with Chinese characters when using stripos?

Will there be problems with Chinese characters when using stripos?

M66 2025-05-31

In PHP programming, stripos() is a very common string search function. It is used to find the first occurrence of a string in the target string without case sensitivity. Many developers may be worried about one issue when dealing with multilingual or strings containing Chinese content:

This article will discuss this issue and verify it through sample code.

1. Basic usage of stripos()

The function signature of stripos() is as follows:

 int|false stripos(string $haystack, string $needle, int $offset = 0)

It returns the position where $needle first appears in $haystack (starting at 0), and if not found, it returns false . This function is not case sensitive.

Example:

 $haystack = "Hello World!";
$needle = "world";
$pos = stripos($haystack, $needle);
echo $pos; // Output 6

2. Do Chinese characters have any impact on stripos()?

The key question is: Can stripos() accurately match when $haystack or $needle include Chinese?

The answer is: it can match normally, but pay attention to the encoding of the string.

PHP's stripos() is matched based on bytes, not based on character levels. This means that when using stripos() to process UTF-8-encoded Chinese strings, if $needle is Chinese, or $haystack contains Chinese characters, it is usually no problem as long as the encoding remains consistent.

3. Actual test verification

Let’s test the behavior of Chinese matching through a few examples:

 // UTF-8 Encoded string
$haystack = "Welcome to visit m66.net website";
$needle = "m66.net";

// Find English
$pos = stripos($haystack, $needle);
echo $pos !== false ? "Found,Location is:$pos" : "Not found";  // Output:Found,Location is:5
 // Find Chinese
$haystack = "Welcome to visit m66.net website";
$needle = "website";

$pos = stripos($haystack, $needle);
echo $pos !== false ? "Found,Location is:$pos" : "Not found";  // Output:Found,Location is:15

As you can see, stripos() works normally whether it is searching for English or Chinese.

4. Things to note

Although stripos() usually has no problem dealing with Chinese, there are still several points to note:

  1. Ensure that the string encoding is consistent : PHP defaults to match by bytes. If $haystack is UTF-8 and $needle is GB2312 and other encoding formats, it may cause matching failure.

  2. It is recommended to use mb_stripos() when processing multi-byte strings : If you need to handle multi-language content more securely, especially East Asian characters (China, Japan and South Korea), it is recommended to use mb_stripos() , which is a function in the mbstring extension, designed for multi-byte strings.

Example:

 $haystack = "Welcome to visit m66.net website";
$needle = "website";

$pos = mb_stripos($haystack, $needle, 0, 'UTF-8');
echo $pos !== false ? "Found,Location is:$pos" : "Not found";

5. Summary

Use stripos() to determine whether a string containing Chinese contains a certain substring will not cause a match error if the encoding is correct. However, for better multilingual compatibility, it is recommended to use mb_stripos() in your project.

In any case, always keep the strings uniformly encoded (usually UTF-8) to avoid unnecessary hassle.