admin 管理员组

文章数量: 1087139


2024年4月13日发(作者:ddcms)

mb_strpos()的用法

mb_strpos() 是 PHP 中的一个函数,用于在字符串中搜索指定

的字符或字符串,并返回第一次出现的位置。与 strpos() 不同

的是,mb_strpos() 是使用多字节编码的字符串。以下是它的

用法:

mb_strpos(string $haystack, string $needle, int $offset = 0, string

$encoding = null): int|false

参数说明:

- haystack:要搜索的字符串。

- needle:要查找的字符或字符串。

- offset:可选参数,从字符串的哪个位置开始搜索,默认为 0。

- encoding:可选参数,指定要使用的字符编码,默认为使用

内部字符编码。

返回值:

如果找到 needle,则返回其在 haystack 中首次出现的位置。如

果未找到,则返回 false。

示例用法:

$haystack = "Hello, world!";

$needle = "world";

$position = mb_strpos($haystack, $needle);

echo $position; // 输出 7

在上面的例子中,我们在字符串 "Hello, world!" 中搜索字符串

"world",并且找到它的位置是 7。


本文标签: 字符串 位置 搜索 使用 字符