1 mixed strpos ( string $haystack
, mixed $needle
[, int $offset
= 0 ] )
查找字符串首次出现的位置,返回 needle
在 haystack
中首次出现的数字位置。
offset是偏移量,也就是$offset的位置开始查找,不能为负。
<?php
// 忽视位置偏移量之前的字符进行查找
$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1); // $pos = 7, 不是 0
?>
stripos()只是忽略大小写,其他与之相同。
2 int strrpos ( string $haystack
, string $needle
[, int $offset
= 0 ] )
计算指定字符串在目标字符串中最后一次出现的位置。
offset可以使负数,表示忽略掉字符串后的offset个字符再进行查找。
<?php
$foo = "0123456789a123456789b123456789c";
var_dump(strrpos($foo, '7', -5)); // 从尾部第 5 个位置开始查找
// 结果: int(17)
var_dump(strrpos($foo, '7', 20)); // 从第 20 个位置开始查找
// 结果: int(27)
var_dump(strrpos($foo, '7', 28)); // 结果: bool(false)
?>
strripos() 忽略大小写。
总结:这个四个函数 ,不过查找位置和查找方向如何,最后得到位置数都是从头算起的。