php中explode與split的區(qū)別介紹
更新時間:2012年10月03日 23:47:59 作者:
今天在使用split時遇到一些問題。還是對函數(shù)理解不深刻,特寫出來做個記號
首先來看下兩個方法的定義:
函數(shù)原型:array split (string $pattern, string $string [, int $limit])
函數(shù)原型:array explode ( string $separator, string $string [, int $limit])
初看沒有啥差別,貌似功能都一樣。我就犯了這個錯誤。 請注意兩個函數(shù)的第一個參數(shù)string $pattern和string separator,一個是$pattern說明是正則字符串,一個是$separator是普通字符串。
看下面的代碼:
$test = end(explode('.', 'abc.txt'));
echo $test;//output txt
換成:
$test1 = end(split('.','abc.txt'));
echo $test1;//no output
用split的正確做法是:加轉義符號
$test1 = end(split('\.','abc.txt'));
echo $test1;//output txt
分析:"." 符號是正則表達式的關鍵字所以split無效,而explode有效。
函數(shù)原型:array split (string $pattern, string $string [, int $limit])
函數(shù)原型:array explode ( string $separator, string $string [, int $limit])
初看沒有啥差別,貌似功能都一樣。我就犯了這個錯誤。 請注意兩個函數(shù)的第一個參數(shù)string $pattern和string separator,一個是$pattern說明是正則字符串,一個是$separator是普通字符串。
看下面的代碼:
復制代碼 代碼如下:
$test = end(explode('.', 'abc.txt'));
echo $test;//output txt
換成:
復制代碼 代碼如下:
$test1 = end(split('.','abc.txt'));
echo $test1;//no output
用split的正確做法是:加轉義符號
復制代碼 代碼如下:
$test1 = end(split('\.','abc.txt'));
echo $test1;//output txt
分析:"." 符號是正則表達式的關鍵字所以split無效,而explode有效。
您可能感興趣的文章:
- PHP使用preg_split()分割特殊字符(元字符等)的方法分析
- PHP 正則表達式之正則處理函數(shù)小結(preg_match,preg_match_all,preg_replace,preg_split)
- php中利用explode函數(shù)分割字符串到數(shù)組
- PHP的explode和implode的使用說明
- php字符串分割函數(shù)explode的實例代碼
- PHP explode()函數(shù)用法、切分字符串
- php使用explode()函數(shù)將字符串拆分成數(shù)組的方法
- php連接函數(shù)implode與分割explode的深入解析
- PHP中explode函數(shù)和split函數(shù)的區(qū)別小結
- PHP中substr()與explode()函數(shù)用法分析
- PHP 字符串分割和比較
- PHP使用preg_split和explode分割textarea存放內容的方法分析
- PHP explode() 函數(shù)
相關文章
解析:php調用MsSQL存儲過程使用內置RETVAL獲取過程中的return值
本篇文章是對php調用MsSQL存儲過程使用內置RETVAL獲取過程中的return值的方法進行了詳細的分析介紹,需要的朋友參考下2013-07-07