strstr()與strpos()函數(shù)怎么在php中使用?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )
1、$haystack被查找的字符串,$needle要查找的內(nèi)容
2、如查找到則返回字符串的一部分,如沒(méi)找到則返回FALSE
3、該函數(shù)區(qū)分大小寫(xiě),如果想要不區(qū)分大小寫(xiě),請(qǐng)使用stristr()
4、如果你僅僅想確定needle是否存在于haystack中請(qǐng)使用速度更快、耗費(fèi)內(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); //如果沒(méi)找到,則返回布爾值 FALSE ?>
運(yùn)行結(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ū)分大小寫(xiě),如果想要不區(qū)分大小寫(xiě),請(qǐng)使用stripos()
4、返回值,如找到的話(huà),返回needle 存在于 haystack 字符串起始的位置(注意字符串位置是從0開(kāi)始,而不是從1開(kāi)始),沒(méi)找到則返回FALSE,但也可能返回等同于 FALSE 的非布爾值
<?php $mystring = 'abc' ; $findme = 'a' ; $pos = strpos($mystring,$findme); echo $pos; //輸出0,既是當(dāng)前a的位置 ?>
運(yùn)行結(jié)果:
0
這里2個(gè)比較相似的函數(shù),在這里簡(jiǎn)單介紹下,只需記住有這個(gè)函數(shù)即可,用時(shí)簡(jiǎn)單看下手冊(cè)。
1、strrpos(),計(jì)算指定字符串在目標(biāo)字符串中最后一次出現(xiàn)的位置
實(shí)例1 使用 ===
<?php
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// 注意這里使用的是 ===。簡(jiǎn)單的 == 不能像我們期待的那樣工作,
// 因?yàn)?nbsp;'a' 是第 0 位置上的(第一個(gè))字符。
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";
}
?>實(shí)例2 使用 !==
<?php
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// 使用 !== 操作符。使用 != 不能像我們期待的那樣工作,
// 因?yàn)?nbsp;'a' 的位置是 0。語(yǔ)句 (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'";
}
?>實(shí)例3 使用位置偏移量
<?php // 忽視位置偏移量之前的字符進(jìn)行查找 $newstring = 'abcdef abcdef'; $pos = strpos($newstring, 'a', 1); // $pos = 7, 不是 0 ?>
注釋
Note: 此函數(shù)可安全用于二進(jìn)制對(duì)象。
2、strripos(),計(jì)算指定字符串在目標(biāo)字符串中最后一次出現(xiàn)的位置(不區(qū)分大小寫(xiě))
php是一個(gè)嵌套的縮寫(xiě)名稱(chēng),是英文超級(jí)文本預(yù)處理語(yǔ)言,它的語(yǔ)法混合了C、Java、Perl以及php自創(chuàng)新的語(yǔ)法,主要用來(lái)做網(wǎng)站開(kāi)發(fā),許多小型網(wǎng)站都用php開(kāi)發(fā),因?yàn)閜hp是開(kāi)源的,從而使得php經(jīng)久不衰。
看完上述內(nèi)容,你們掌握strstr()與strpos()函數(shù)怎么在php中使用的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
本文標(biāo)題:strstr()與strpos()函數(shù)怎么在php中使用-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://chinadenli.net/article24/ddpjje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、品牌網(wǎng)站制作、建站公司、商城網(wǎng)站、動(dòng)態(tài)網(wǎng)站、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容