- strstr -- 查找字符串的首次出現(xiàn),返回字符串從第一次出現(xiàn)的位置開始到該字符串的結(jié)尾或開始。
- stristr -- strstr 函數(shù)的忽略大小寫版本
- strchr -- strstr 函數(shù)的別名
- strrchr -- 查找字符串的最后一次出現(xiàn),返回字符串從最后一次出現(xiàn)的位置開始到該字符串的結(jié)尾。
strstr
查找字符串的首次出現(xiàn),返回字符串從第一次出現(xiàn)的位置開始到該字符串的結(jié)尾或開始。
mixed strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )
參數(shù)說明
haystack
在該字符串中進(jìn)行查找。
needle
如果 needle 不是一個(gè)字符串,那么它將被轉(zhuǎn)換為整型并被視為字符的順序值來使用。
before_needle
若為 TRUE,strstr() 將返回 needle 在 haystack 中的位置之前的部分。
返回值
成功:返回字符串 needle 之前或之后的一部分
失敗:如果沒找到 needle,將返回 FALSE。
注意
- 該函數(shù)區(qū)分大小寫
- 如果你僅僅想確定 needle 是否存在于 haystack 中,請(qǐng)使用速度更快、耗費(fèi)內(nèi)存更少的 strpos() 函數(shù)
示例
<?php
/*【 needle 為單個(gè)字符 】 */
$email = 'name@example.com';
$domain = strstr($email, '@');
echo $domain; // 打印 @example.com
$user = strstr($email, '@', true); // 從 PHP 5.3.0 起
echo $user; // 打印 name
?>
<?php
/*【 needle 為數(shù)字 】 */
$email = 'name@example.com'; //字母a的 ASCII碼為 97
$behind = strstr($email, 97);
echo $behind; // 打印 ame@example.com
$front = strstr($email, 97, true); // 從 PHP 5.3.0 起
echo $front; // 打印 n
?>
<?php
/*【 needle 為字符串 】 */
$email = 'name@example.com';
$behind = strstr($email, 'ex');
echo $behind; // 打印 example.com
$front = strstr($email, 'ex', true); // 從 PHP 5.3.0 起
echo $front; // 打印 name@
*/
?>
<?php
/*【 needle 為字符串 】 */
$email = 'name@example.com';
$behind = strstr($email, 'ab');
echo $behind; // 返回 false
$front = strstr($email, 'ab', true); // 從 PHP 5.3.0 起
echo $front; // 返回 false
*/
?>
stristr
strstr() 函數(shù)的忽略大小寫版本
mixed stristr ( string $haystack , mixed $needle [, bool $before_needle = false ] )
該函數(shù)與 strstr() 唯一的區(qū)別就是不區(qū)分大小寫。其他可參考strstr()
<?php
$email = 'name@example.com';
$behind = stristr($email, 'A');
echo $behind; // 打印 ame@example.com
$front = stristr($email, 'A', true); // 從 PHP 5.3.0 起
echo $front; // 打印 n
?>
strchr
strstr() 函數(shù)的別名
mixed strchr ( string $haystack , mixed $needle [, bool $before_needle = false ] )
該函數(shù)等同 strstr() 。其他可參考strstr()
$email = 'name@example.com';
$behind = strchr($email, 'a');
echo $behind; // 打印 ame@example.com
$front = strchr($email, 'a', true); // 從 PHP 5.3.0 起
echo $front; // 打印 n
?>
strrchr
查找字符串的最后一次出現(xiàn),返回字符串從最后一次出現(xiàn)的位置開始到該字符串的結(jié)尾。
mixed strrchr ( string $haystack , mixed $needle )
參數(shù)說明
haystack
在該字符串中進(jìn)行查找。
needle
如果 needle 包含了不止一個(gè)字符,那么僅使用第一個(gè)字符。該行為不同于 strstr()。
如果 needle 不是一個(gè)字符串,那么將被轉(zhuǎn)化為整型并被視為字符順序值。
返回值
成功:返回字符串 needle 之后的一部分
失敗:如果沒找到 needle,將返回 FALSE。
示例
<?php
/*【 needle 為字符 】 */
$email = 'name@example.com';
$behind = strrchr($email, 'a');
echo $behind; // 打印 ample.com
?>
/*【 needle 為字符串 】 */
$email = 'name@example.com';
$behind = strrchr($email, 'am');
echo $behind; // 打印 ample.com
?>
<?php
/*【 needle 為數(shù)字 】 */
$email = 'name@example.com';
$behind = strrchr($email, 97);
echo $behind; // 打印 ample.com
?>
OneAPM for PHP 能夠深入到所有 PHP 應(yīng)用內(nèi)部完成應(yīng)用性能管理 能夠深入到所有 PHP 應(yīng)用內(nèi)部完成應(yīng)用性能管理和監(jiān)控,包括代碼級(jí)別性能問題的可見性、性能瓶頸的快速識(shí)別與追溯、真實(shí)用戶體驗(yàn)監(jiān)控、服務(wù)器監(jiān)控和端到端的應(yīng)用性能管理。想閱讀更多技術(shù)文章,請(qǐng)?jiān)L問 OneAPM 官方技術(shù)博客。
本文轉(zhuǎn)自 OneAPM 官方博客