PHP字符串函數之 strpos stripos strrpos strripos
- strpos – 查找字符串首次出現的位置
- stripos – 查找字符串首次出現的位置(不區分大小寫)
- strrpos – 計算指定字符串在目標字符串中最後一次出現的位置
- strripos – 計算指定字符串在目標字符串中最後一次出現的位置(不區分大小寫)
strpos
查找字符串首次出現的位置
mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
在 PHP7 源碼中該函數實現在string.c文件的1950行附近
參數說明
haystack
在該字符串中進行查找。
needle
如果 needle 不是一個字符串,那麼它將被轉換爲整型並被視爲字符的順序值。
offset
如果提供了此參數,搜索會從字符串該字符數的起始位置開始統計
和 strrpos()、 strripos()不一樣,這個偏移量不能是負數。
返回值
成功:返回 needle 存在於 haystack 字符串起始的位置(獨立於 offset)
失敗:如果沒找到 needle,將返回 FALSE。
注意
- 字符串位置是從0開始,而不是從1開始的
- 此函數可能返回布爾值 FALSE,但也可能返回等同於 FALSE 的非布爾值
應使用 === 運算符或 !== 來測試此函數的返回值
示例
<?php
// 忽視位置偏移量之前的字符進行查找
$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1); // $pos = 7, 不是 0
?>
<?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
}
?>
<?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,
*/
?>
stripos
查找字符串首次出現的位置(不區分大小寫)
mixed stripos ( string $haystack , string $needle [, int $offset = 0 ] )
在 PHP7 源碼中該函數實現在string.c文件的2008行附近
該函數與 strpos 唯一的區別就是不區分大小寫。其他可參考strpos
<?php
$haystack = 'My name is Jay, age 28';
$needle = 'A';
var_dump(stripos($haystack, $needle));
/*
結果是 4
*/
?>
strrpos
計算指定字符串在目標字符串中最後一次出現的位置
mixed strrpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
在 PHP7 源碼中該函數實現在string.c文件的2068行附近
參數說明
haystack
在該字符串中進行查找。
needle
如果 needle 不是一個字符串,那麼它將被轉換爲整型並被視爲字符的順序值。
offset
如果提供了此參數,搜索會從字符串該字符數的起始位置開始統計,可以是負數
返回值
成功:返回 needle 存在於 haystack 字符串起始的位置(獨立於 offset)
失敗:如果沒找到 needle,將返回 FALSE。
注意
- 字符串位置是從0開始,而不是從1開始的
- 此函數可能返回布爾值 FALSE,但也可能返回等同於 FALSE 的非布爾值
應使用 === 運算符或 !== 來測試此函數的返回值
示例
<?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
strripos
計算指定字符串在目標字符串中最後一次出現的位置(不區分大小寫)
mixed strripos ( string $haystack , mixed $needle [, int $offset = 0 ] )
在 PHP7 源碼中該函數實現在string.c文件的2137行附近
該函數與 strrpos 唯一的區別就是不區分大小寫。其他可參考strrpos
<?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)";
}
?>