php中explode與split的區(qū)別介紹
更新時(shí)間:2012年10月03日 23:47:59 作者:
今天在使用split時(shí)遇到一些問題。還是對(duì)函數(shù)理解不深刻,特寫出來做個(gè)記號(hào)
首先來看下兩個(gè)方法的定義:
函數(shù)原型:array split (string $pattern, string $string [, int $limit])
函數(shù)原型:array explode ( string $separator, string $string [, int $limit])
初看沒有啥差別,貌似功能都一樣。我就犯了這個(gè)錯(cuò)誤。 請(qǐng)注意兩個(gè)函數(shù)的第一個(gè)參數(shù)string $pattern和string separator,一個(gè)是$pattern說明是正則字符串,一個(gè)是$separator是普通字符串。
看下面的代碼:
$test = end(explode('.', 'abc.txt'));
echo $test;//output txt
換成:
$test1 = end(split('.','abc.txt'));
echo $test1;//no output
用split的正確做法是:加轉(zhuǎn)義符號(hào)
$test1 = end(split('\.','abc.txt'));
echo $test1;//output txt
分析:"." 符號(hào)是正則表達(dá)式的關(guān)鍵字所以split無效,而explode有效。
函數(shù)原型:array split (string $pattern, string $string [, int $limit])
函數(shù)原型:array explode ( string $separator, string $string [, int $limit])
初看沒有啥差別,貌似功能都一樣。我就犯了這個(gè)錯(cuò)誤。 請(qǐng)注意兩個(gè)函數(shù)的第一個(gè)參數(shù)string $pattern和string separator,一個(gè)是$pattern說明是正則字符串,一個(gè)是$separator是普通字符串。
看下面的代碼:
復(fù)制代碼 代碼如下:
$test = end(explode('.', 'abc.txt'));
echo $test;//output txt
換成:
復(fù)制代碼 代碼如下:
$test1 = end(split('.','abc.txt'));
echo $test1;//no output
用split的正確做法是:加轉(zhuǎn)義符號(hào)
復(fù)制代碼 代碼如下:
$test1 = end(split('\.','abc.txt'));
echo $test1;//output txt
分析:"." 符號(hào)是正則表達(dá)式的關(guān)鍵字所以split無效,而explode有效。
您可能感興趣的文章:
- PHP使用preg_split()分割特殊字符(元字符等)的方法分析
- PHP 正則表達(dá)式之正則處理函數(shù)小結(jié)(preg_match,preg_match_all,preg_replace,preg_split)
- php中利用explode函數(shù)分割字符串到數(shù)組
- PHP的explode和implode的使用說明
- php字符串分割函數(shù)explode的實(shí)例代碼
- PHP explode()函數(shù)用法、切分字符串
- php使用explode()函數(shù)將字符串拆分成數(shù)組的方法
- php連接函數(shù)implode與分割explode的深入解析
- PHP中explode函數(shù)和split函數(shù)的區(qū)別小結(jié)
- PHP中substr()與explode()函數(shù)用法分析
- PHP 字符串分割和比較
- PHP使用preg_split和explode分割textarea存放內(nèi)容的方法分析
- PHP explode() 函數(shù)
相關(guān)文章
解析:php調(diào)用MsSQL存儲(chǔ)過程使用內(nèi)置RETVAL獲取過程中的return值
本篇文章是對(duì)php調(diào)用MsSQL存儲(chǔ)過程使用內(nèi)置RETVAL獲取過程中的return值的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07PHP基于方差和標(biāo)準(zhǔn)差計(jì)算學(xué)生成績的穩(wěn)定性示例
這篇文章主要介紹了PHP基于方差和標(biāo)準(zhǔn)差計(jì)算學(xué)生成績的穩(wěn)定性操作,涉及PHP數(shù)學(xué)運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-07-07PHP利用ueditor實(shí)現(xiàn)上傳圖片添加水印
在上傳圖片時(shí),有時(shí)需要添加水印。如果每個(gè)都用PS添加的話,會(huì)有些麻煩。本文將為大家介紹PHP如何利用ueditor實(shí)現(xiàn)上傳圖片添加水印,感興趣的可以了解一下2022-07-07