PHP 8.3.4 Released!

mb_stristr

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

mb_stristr大小写不敏感地查找字符串在另一个字符串里的首次出现

说明

mb_stristr(
    string $haystack,
    string $needle,
    bool $before_needle = false,
    ?string $encoding = null
): string|false

mb_strstr() 查找了 needlehaystack 中首次的出现并返回 haystack 的一部分。 和 mb_strstr() 不同的是,mb_stristr() 是大小写不敏感的。 如果 needle 没有找到,它将返回 false

参数

haystack

要获取 needle 首次出现的字符串。

needle

haystack 中查找这个字符串。

before_needle

决定这个函数返回 haystack 的哪一部分。 如果设置为 true,它返回 haystack 中从开始到 needle 出现位置的所有字符(不包括 needle)。 如果设置为 false,它返回 haystackneedle 出现位置到最后的所有字符(包括了 needle)。

encoding

要使用的字符编码名称。 如果省略该参数,将使用内部字符编码。

返回值

返回 haystack 的一部分,或者 needle 没找到则返回 false

更新日志

版本 说明
8.0.0 现在 needle 接受空字符串。
8.0.0 现在 encoding 可以为 null。

参见

  • stristr() - strstr 函数的忽略大小写版本
  • strstr() - 查找字符串的首次出现
  • mb_strstr() - 查找字符串在另一个字符串里的首次出现

add a note

User Contributed Notes 1 note

up
0
nowfel dot terki at mailfence dot com
1 year ago
Be aware that if needle is an empty string, mb_stristr return the haystack by default.

For exemple:

<?php
if (mb_stristr("foo", "")) {
echo
"We enter in condition";
}
?>

Because in the above exemple the return of mb_stristr is "foo".

So if we do not want this kind of behaviour, we must set the third argument, ($before_needle) to true.

<?php
if (mb_stristr("foo", "", true)) {
echo
"We do not enter in condition";
}
?>

It can be useful to know it, specially when needle is dynamic.
To Top