strpos – 查找字符串首次出现的位置,strpos() 函数对大小写敏感(区分大小写)。
stripos – 查找字符串首次出现的位置(不区分大小写)
strrpos – 计算指定字符串在目标字符串中最后一次出现的位置(区分大小写)
strripos – 计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写)
查找字符串首次出现的位置
</>code
- mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )1
在 PHP7 源码中该函数实现在string.c文件的1950行附近
haystack
在该字符串中进行查找。
needle
如果 needle 不是一个字符串,那么它将被转换为整型并被视为字符的顺序值。
offset
如果提供了此参数,搜索会从字符串该字符数的起始位置开始统计
和 strrpos()、 strripos()不一样,这个偏移量不能是负数。
成功:返回 needle 存在于 haystack 字符串起始的位置(独立于 offset)
失败:如果没找到 needle,将返回 FALSE。
字符串位置是从0开始,而不是从1开始的
此函数可能返回布尔值 FALSE,但也可能返回等同于 FALSE 的非布尔值
应使用 === 运算符或 !== 来测试此函数的返回值
</>code
- <?php
- // 忽视位置偏移量之前的字符进行查找
- $newstring = 'abcdef abcdef';
- $pos = strpos($newstring, 'a', 1); // $pos = 7, 不是 0
- ?>
</>code
- <?php
- $mystring = 'abc';
- $findme = 'a';
- $pos = strpos($mystring, $findme); //$pos = 0
- // 注意这里使用的是 ===。简单的 == 不能像我们期待的那样工作,
- // 因为 'a' 是第 0 位置上的(第一个)字符。
- if ($pos === false) {
- echo "没有找到字符串 '$findme' ";
- } else {
- echo "字符串 '$findme' 在字符串 '$mystring' 中被发现"
- echo "在其中的位置是 $pos"; //$pos = 0
- }
- ?>
</>code
- <?php$haystack = 'My name is Jay, age 28';$needle = 8;
- var_dump(strpos($haystack, $needle));/*
- 结果是 false,因为这里的 $needl是数字8,不是字符8,PHP会转成相应的ASII
- 8的ASII是 ^H, 自然是找不到的
- */?><?php$haystack = 'My name is Jay, age 28';$needle = 97;
- var_dump(strpos($haystack, $needle));/*
- 结果是 4,因为97的ASII是 字母a,
- */?>
查找字符串首次出现的位置(不区分大小写)
</>code
- mixed stripos ( string $haystack , string $needle [, int $offset = 0 ] )1
在 PHP7 源码中该函数实现在string.c文件的2008行附近
该函数与 strpos 唯一的区别就是不区分大小写。其他可参考strpos
</>code
- <?php$haystack = 'My name is Jay, age 28';$needle = 'A';
- var_dump(stripos($haystack, $needle));/*
- 结果是 4
- */?>
</>code
- <?php
- $findme = 'a';
- $mystring1 = 'xyz';
- $mystring2 = 'ABC';
- $pos1 = stripos($mystring1, $findme);
- $pos2 = stripos($mystring2, $findme);
- // 'a' 当然不在 'xyz' 中
- if ($pos1 === false) {
- echo "The string '$findme' was not found in the string '$mystring1'";
- }
- // 注意这里使用的是 ===。简单的 == 不能像我们期望的那样工作,
- // 因为 'a' 的位置是 0(第一个字符)。
- if ($pos2 !== false) {
- echo "We found '$findme' in '$mystring2' at position $pos2";
- }
- ?>
计算指定字符串在目标字符串中最后一次出现的位置
</>code
- mixed strrpos ( string $haystack , mixed $needle [, int $offset = 0 ] )1
在 PHP7 源码中该函数实现在string.c文件的2068行附近
haystack
在该字符串中进行查找。
needle
如果 needle 不是一个字符串,那么它将被转换为整型并被视为字符的顺序值。
offset
如果提供了此参数,搜索会从字符串该字符数的起始位置开始统计,可以是负数
成功:返回 needle 存在于 haystack 字符串起始的位置(独立于 offset)
失败:如果没找到 needle,将返回 FALSE。
字符串位置是从0开始,而不是从1开始的
此函数可能返回布尔值 FALSE,但也可能返回等同于 FALSE 的非布尔值
应使用 === 运算符或 !== 来测试此函数的返回值
</>code
- <?php
- $foo = "012345678901234567890123456789";
- //从尾部第 3 个位置开始查找,
- //结果: int(27)
- var_dump(strrpos($foo, '7', -3));
- //从尾部第 4 个位置开始查找,
- //结果: int(17)
- var_dump(strrpos($foo, '7', -4));
- //从第 20 个位置开始查找
- //结果: int(27)
- var_dump(strrpos($foo, '7', 20));
- //结果: bool(false)
- var_dump(strrpos($foo, '7', 28));
- ?>
- 可能有同学对上面的 -3那个例子看不明白
- 为什么上面的 -3,查找到的'7',最后出现的位置仍然是27,而不是17
- 这个-3该怎么计算呢?
- 其实很简单,我们观察一下$foo, 长度为30,在C语言中用字符数组来存的话就是0到29
- 在PHP内部是使用$foo的长度30加上偏移-3等于27,然后在字符数组0到27中来查找。
- 0到27也就是字符串 0123456789012345678901234567,所以为'7'最后一次出现的位置是27
计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写)
</>code
- mixed strripos ( string $haystack , mixed $needle [, int $offset = 0 ] )1
在 PHP7 源码中该函数实现在string.c文件的2137行附近
该函数与 strrpos 唯一的区别就是不区分大小写。其他可参考strrpos
</>code
- <?php
- $haystack = 'ababcd';
- $needle = 'aB';
- $pos = strripos($haystack, $needle);
- if ($pos === false) {
- echo "Sorry, we did not find ($needle) in ($haystack)";
- } else {
- //结果找到了,位置为2
- echo "Congratulations!\n";
- echo "We found the last ($needle) in ($haystack) at position ($pos)";
- }
- ?>
PHP strripos note #3
Suppose you just need a stripos function working backwards expecting that strripos does this job, you better use the following code of a custom function named strbipos:
<?php
function strbipos($haystack="", $needle="", $offset=0) {
// Search backwards in $haystack for $needle starting from $offset and return the position found or false
$len = strlen($haystack);
$pos = stripos(strrev($haystack), strrev($needle), $len - $offset -1);
return ( ($pos === false) ? false : $len - strlen($needle) - $pos );
}
// Test
$body = "01234Xy7890XYz456xy90";
$str = "xY";
$len = strlen($body);
echo "TEST POSITIVE offset VALUES IN strbipos<br>";
for ($i = 0; $i < $len; $i++) {
echo "Search in [$body] for [$str] starting from offset [$i]: [" .strbipos($body, $str, $i) . "]<br>";
}
?>
Note that this function does exactly what it says and its results are different comparing to PHP 5 strripos function.
PHP strripos note #4
OK, I guess this will be the final function implementation for PHP 4.x versions ( my previous posts are invalid )
<?php
if(!function_exists("stripos")){
function stripos( $str, $needle, $offset = 0 ){
return strpos( strtolower( $str ), strtolower( $needle ),$offset );
}/* endfunction stripos */
}/* endfunction exists stripos */
if(!function_exists("strripos")){
function strripos( $haystack, $needle, $offset = 0 ) {
if( !is_string( $needle ) )$needle = chr( intval( $needle ) );
if( $offset < 0 ){
$temp_cut = strrev( substr( $haystack, 0, abs($offset) ) );
}
else{
$temp_cut = strrev( substr( $haystack, 0, max( (strlen($haystack) - $offset ), 0 ) ) );
}
if( ( $found = stripos( $temp_cut, strrev($needle) ) ) ===FALSE )return FALSE;
$pos = ( strlen( $haystack ) - ( $found + $offset + strlen($needle ) ) );
return $pos;
}/* endfunction strripos */
}/* endfunction exists strripos */
?>
PHP strripos note #5
Oops, I forgot to return "false" if the needle is not found. Here is the proper function.
<?php
if(!function_exists("strripos")){
function strripos($haystack, $needle, $offset=0) {
if($offset<0){
$temp_cut = strrev( substr( $haystack, 0, abs($offset) ) );
}
else{
$temp_cut = strrev( substr( $haystack, $offset ) );
}
$pos = strlen($haystack) - (strpos($temp_cut, strrev($needle)) +$offset + strlen($needle));
if ($pos == strlen($haystack)) { $pos = 0; }
if(strpos($temp_cut, strrev($needle))===false){
return false;
}
else return $pos;
}/* endfunction strripos*/
}/* endfunction exists strripos*/
?>
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛