PHP stripos()函数用于在字符串中查找子字符串的第一次出现的位置(不区分大小写)。
函数的语法为:
stripos(string $haystack, mixed $needle [, int $offset = 0]): int|false
参数说明:
-
$haystack
:被搜索的字符串。 -
$needle
:要搜索的子字符串。 -
$offset
:可选参数,指定搜索起始位置,默认为0。
返回值:
-
如果找到子字符串,则返回第一次出现的位置索引(从0开始计数)。
-
如果未找到子字符串,则返回false。
示例:
$haystack = "Hello, world!"; $needle = "world"; $position = stripos($haystack, $needle); if ($position !== false) { echo "子字符串 '$needle' 在字符串 '$haystack' 的第一次出现位置是 $position"; } else { echo "未找到子字符串 '$needle' 在字符串 '$haystack' 中"; }
输出结果为:
子字符串 'world' 在字符串 'Hello, world!' 的第一次出现位置是 7