In PHP, stripos and mb_stripos are both functions used to find the location of substrings, but they have some significant differences. This article will discuss their differences, applicable scenarios and performance differences in detail.
stripos : This is a built-in function in PHP to find where a string first appears in another string. It is case-insensitive and is based on single-byte encoding, suitable for ordinary ASCII strings.
$haystack = "Hello world!";
$needle = "world";
$pos = stripos($haystack, $needle); // turn out 6
mb_stripos : This function comes from multi-byte string extension (MBString). Its function is similar to stripos . It is also used to find the location of a substring and is case-insensitive. Unlike stripos , mb_stripos is suitable for handling multibyte character sets, such as UTF-8, GBK, etc. It is an optimization for multibyte encoded strings.
$haystack = "Hello,world!";
$needle = "world";
$pos = mb_stripos($haystack, $needle); // turn out 3
Coding support:
stripos only works with single-byte encoded strings (such as ASCII), which means it does not work with strings that handle UTF-8 or other multibyte character sets.
mb_stripos is designed specifically for multibyte encoding (such as UTF-8, GBK, etc.), so strings containing multibyte characters can be processed correctly.
Performance differences:
For ordinary ASCII strings, stripos performs better than mb_stripos because mb_stripos requires additional overhead to handle multibyte character sets.
If the processed string is UTF-8 or other multibyte character sets, mb_stripos is essential because stripos cannot handle these character sets correctly.
Scenarios using stripos :
String encoding is single byte (such as ASCII encoding), and you can use stripos to efficiently find substrings.
Stripos is a suitable choice when dealing with English characters or simple ASCII strings.
Scenarios using mb_stripos :
String encoding is multi-byte, such as UTF-8, GBK, Big5, etc. You need to use mb_stripos to correctly find substrings.
When processing strings containing multi-byte characters such as Chinese, Japanese, and Korean, you must use mb_stripos .
Stripos is a lighter function in terms of performance, especially when the string is ASCII encoded, it will be faster because it does not need to consider the byte length of the character. However, when dealing with multibyte characters (such as UTF-8-encoded Chinese strings), mb_stripos is required, which will be slightly slower because of the complexity of multibyte characters.
<?php
$haystack = "This is a test string.";
$needle = "test";
$position = stripos($haystack, $needle);
if ($position !== false) {
echo "Found '$needle' at position $position";
} else {
echo "Not found";
}
?>
<?php
$haystack = "Hello,world!";
$needle = "world";
$position = mb_stripos($haystack, $needle);
if ($position !== false) {
echo "Found '$needle' at position $position";
} else {
echo "Not found";
}
?>
Stripos is suitable for single-byte character sets (such as ASCII) and has good performance.
mb_stripos is suitable for multibyte character sets (such as UTF-8, GBK, etc.), which handles these characters correctly.
Stripos is more efficient when dealing with English or ASCII characters; mb_stripos is used when dealing with Chinese or other multibyte characters.