本篇文章給大家帶來的內容是關於php正則表達常用函數的總結(程式碼範例),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所協助。
php中常用的Regex函數
* preg_match()
* preg_match_all()
* preg_replace()
* preg_filter()
* preg_grep()
* preg_split()
* preg_quote()
接下來對比講解:
講解中 $pattern 通常Regex $subject 通常表示目標處理資料
定義一個方法 方便查看資料類型:
function show($val = null){ if(empty($val)){ //列印空 echo 'mull'; }elseif(is_array($val) || is_object($val)){ //列印數組或者對象 echo '<pre>'; print_r($val); echo '</pre>'; }else{ //列印string int float echo $val; }}
preg_match()與preg_match_all()
/*方法簡單說明*/ preg_match($pattern,$subject,[array &$matches]) //匹配一次 第三個參數可選 匹配結果放置matches數組 preg_match_all($pattern,$subject,array &matches) //匹配多次 三參必選 匹配結果放置matches數組 $pattern = '/[0-9]/'; $subject='b5fg4sgf3sd2f1g'; $m1 =preg_match($pattern,$subject,$arr1);show($m1);show($arr1);/* 1Array( [0] => 5) */ $m2 =preg_match_all($pattern,$subject,$arr2);show($m2);show($arr2);/* 5Array( [0] => Array ( [0] => 5 [1] => 4 [2] => 3 [3] => 2 [4] => 1 )) */
2.preg_replace()與preg_filter()
/*方法簡單說明*//* * * preg_replace與preg_filter * preg_replace($pattern,$replacement,$subject) * //正則替換函數被替換對象是數組 返回全部//正則替換函數 被替換對象是數組 僅返回被替換元素 * 聯想:str_repalce() * echo str_replace("world","Shanghai","Hello world!"); * //得到Hello Shanghai!//第一測 字串中替換字串 結論:此處preg_replace()與preg_filter()無區別$pattern = '/[0-9]/';$subject='b65fg4sgf3sd2f1g';$replacement = '_';$str1 = preg_replace($pattern,$replacement,$subject);$str2 = preg_filter($pattern,$replacement,$subject);echo '<hr>';show($str1);echo '<br>';show($str2);/*b__fg_sgf_sd_f_gb__fg_sgf_sd_f_g */ //第二測 字串中根據數組正則 替換 結論:此處preg_replace()與preg_filter()無區別 $pattern = array('/[123]/','/[456]/','/[789]/'); $subject='b123fg3456sgf3789f1g'; $replacement = array('天','地','人'); $str3 = preg_replace($pattern,$replacement,$subject); $str4 = preg_filter($pattern,$replacement,$subject); echo '<hr>';show($str3);echo '<br>';show($str4);/*b天天天fg天地地地sgf天人人人f天gb天天天fg天地地地sgf天人人人f天g */ //第三測 數組中根據數組正則 替換 結論:此處展示preg_repalce()與preg_filter()區別 $pattern = array('/[123]/','/[456]/','/[789]/'); $subject=array('b','fg3','456','sgf','789','fg'); $replacement = array('天','地','人'); $str5 = preg_replace($pattern,$replacement,$subject); $str6 = preg_filter($pattern,$replacement,$subject); echo '<hr>';show($str5);echo '<br>';show($str6);/*Array( [0] => b [1] => fg天 [2] => 地地地 [3] => sgf [4] => 人人人 [5] => fg)Array( [1] => fg天 [2] => 地地地 [4] => 人人人) */
3 . preg_grep($pattern, array $subject) //此處$subject只可以為一維數組
/*函數說明*/preg_grep($pattern,array $input)//只可以匹配一維數組 返回匹配成功的數組元素可以理解preg_grep()為閹割版 preg_filter()只做了匹配 不作替換$pattern = '/[0-9]/';$subject=array('b','fg3','456','sgf','789','fg');$arr = preg_grep($pattern,$subject);show($arr);/*Array( [1] => fg3 [2] => 456 [4] => 789) */
4 . preg_split($pattern,$subject)//一匹配成功為切割點切割字串成數組
可以理解preg_split()升級版的explode()
$pattern = '/[0-9]/';$subject='bfg3456sgf789fg';$arr = preg_split($pattern,$subject);show($arr);/*Array( [0] => bfg [1] => [2] => [3] => [4] => sgf [5] => [6] => [7] => fg) */
5 . preg_quote($str) 正則運算子轉譯
/* * preg_quote($str) 正則運算子轉譯 * . \ + * ? [ ^ ] $ () {} = ! < > | : - * */ echo '<hr>'; $str = "qewr{asdf}[123]"; $str =preg_quote($str);print_r($str);/*qewr\{asdf\}\[123\]*/
簡單總結通性:
preg_ 開頭
除preg_quote函數以外,第一個參數都是Regex
preg_match 表單驗證等
preg_replace 非法詞語過濾等