PHP正則表達式函數

轉自https://www.cnblogs.com/sgm4231/p/9782271.html
php中經常使用的正則表達式函數
* preg_match() * preg_match_all() * preg_replace() * preg_filter() * preg_grep() * preg_split() * preg_quote() 接下來對比講解: 講解中 $pattern 一般表示正則表達式
$subject 一般表示目標處理數據 定義一個方法 方便查看數據類型:
複製代碼
function show($val = null){ if(empty($val)){ //打印空 echo 'null'; }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); /* 1 Array ( [0] => 5 ) */

$m2 =preg_match_all($pattern,$subject,$arr2); show($m2); show($arr2); /* 5 Array ( [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_g b__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天g b每天天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函數之外,第一個參數都是正則表達式 preg_match 表單驗證等 preg_replace 非法詞語過濾等
相關文章
相關標籤/搜索