在PHP中,字符串函数 strpos() 用来查找一个字符串在另一个字符串中首次出现的位置。
函数语法:
strpos ( string $str , string $find [, int $start = 0 ] ) : int
函数参数说明:
参数 | 描述 |
---|---|
str | 必需。规定被搜索的字符串。 |
find | 必需。规定要查找的字符。 |
start | 可选。规定开始搜索的位置。 |
strpos() 用来查找一个字符串在另一个字符串中首次出现的索引位置,和 stripos() 函数不同的是,该函数查找时区分大小写,同时注意搜索的字符串位置是从0开始,而不是从1开始的。
举例:
<?php // 查找字符串首次出现的位置 $rawStr = 'hello world'; $res = strpos($rawStr, 'l', 6); echo '字符l首次出现的位置:' . $res;
以上代码输出如下:
字符l首次出现的位置:9