php常用字符串查找函數(shù)strstr()與strpos()實例分析
本文實例講述了php常用字符串查找函數(shù)strstr()與strpos()。分享給大家供大家參考,具體如下:
一句話使用strpos判斷 ===
或!==
,這樣才能達到預(yù)期的效果,性能要比strstr要好,只是判斷是否包含某個字符串就用這個了。
string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )
1、$haystack被查找的字符串,$needle要查找的內(nèi)容
2、如查找到則返回字符串的一部分,如沒找到則返回FALSE
3、該函數(shù)區(qū)分大小寫,如果想要不區(qū)分大小寫,請使用 stristr()
4、如果你僅僅想確定needle是否存在于haystack中請使用速度更快、耗費內(nèi)存更少的strpos()
函數(shù)
<?php $email = 'name@example.com'; $domain = strstr($email,'@'); $name = strstr($email,'@',TRUE); $no_con = strstr($email,'99'); echo $domain; //輸出 @example.com echo $name; //輸出name 從 PHP 5.3.0 起 var_dump($no_con); //如果沒找到,則返回布爾值 FALSE ?>
運行結(jié)果:
@example.com
name
bool(false)
mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
1、$haystack被查找的字符串,$needle要查找的內(nèi)容
2、返回 needle 在 haystack 中首次出現(xiàn)的數(shù)字位置
3、該函數(shù)區(qū)分大小寫,如果想要不區(qū)分大小寫,請使用 stripos()
4、返回值,如找到的話,返回needle 存在于 haystack 字符串起始的位置(注意字符串位置是從0開始,而不是從1開始),沒找到則返回FALSE,但也可能返回等同于 FALSE 的非布爾值
<?php $mystring = 'abc' ; $findme = 'a' ; $pos = strpos($mystring,$findme); echo $pos; //輸出0,既是當(dāng)前a的位置 ?>
運行結(jié)果:
0
這里2個比較相似的函數(shù),在這里簡單介紹下,只需記住有這個函數(shù)即可,用時簡單看下手冊。
1、strrpos()
,計算指定字符串在目標(biāo)字符串中最后一次出現(xiàn)的位置
實例1 使用 ===
<?php $mystring = 'abc'; $findme = 'a'; $pos = strpos($mystring, $findme); // 注意這里使用的是 ===。簡單的 == 不能像我們期待的那樣工作, // 因為 'a' 是第 0 位置上的(第一個)字符。 if ($pos === false) { echo "The string '$findme' was not found in the string '$mystring'"; } else { echo "The string '$findme' was found in the string '$mystring'"; echo " and exists at position $pos"; } ?>
實例2 使用 !==
<?php $mystring = 'abc'; $findme = 'a'; $pos = strpos($mystring, $findme); // 使用 !== 操作符。使用 != 不能像我們期待的那樣工作, // 因為 'a' 的位置是 0。語句 (0 != false) 的結(jié)果是 false。 if ($pos !== false) { echo "The string '$findme' was found in the string '$mystring'"; echo " and exists at position $pos"; } else { echo "The string '$findme' was not found in the string '$mystring'"; } ?>
實例3 使用位置偏移量
<?php // 忽視位置偏移量之前的字符進行查找 $newstring = 'abcdef abcdef'; $pos = strpos($newstring, 'a', 1); // $pos = 7, 不是 0 ?>
注釋
Note: 此函數(shù)可安全用于二進制對象。
2、strripos()
,計算指定字符串在目標(biāo)字符串中最后一次出現(xiàn)的位置(不區(qū)分大小寫)
總結(jié):注意這幾個函數(shù)如果沒找到時則會返回FALSE,故在判斷兩邊是否相等時候(if),注意兩邊的類型,以上幾個函數(shù),是在PHP中比較常用的字符串查找函數(shù)了,如需更強大功能的話,如郵箱、手機號的匹配、驗證的話,則需借助正則表達式完成。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php常用函數(shù)與技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
PHP連接SQLServer2005的實現(xiàn)方法(附ntwdblib.dll下載)
為了php連接sql2005 ,我在網(wǎng)絡(luò)上找了一大堆資料在我的csdn博客中.晚上3:05分時候終于搞定了2012-07-07php中g(shù)et_meta_tags()、CURL與user-agent用法分析
這篇文章主要介紹了php中g(shù)et_meta_tags()、CURL與user-agent用法,以實例形式較為詳細的分析了get_meta_tags()、CURL與user-agent使用時的注意事項與用法,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-12php環(huán)境配置之CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI比較?
什么是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?在php運行環(huán)境配置中用得到,究竟哪種更適合網(wǎng)站。2011-10-10