正則表達式是在日常開發中經常用到的,通常一些使用頻率過高的正則表達式都是直接粘貼復制,對於基礎正則的使用還是要銘記於心的,今天抽時間整理一些php正則表達式的用法。
一、php中常用的正則表達式函數
preg_match() 與 preg_match_all()
1、preg_match ( $pattern, $subject, [ array &$matchs ] ); //執行匹配正則表達式
2、preg_match_all ( $pattern, $subject, array &$matches ); //執行一個全局正則表達式匹配
返回結果:匹配到結果的次數;
參數說明:
pattern 要搜索的模式,字符串類型
subject 輸入字符串
matches 對於第一個函數,如果提供了參數matches,它將被填充為搜索結果。$matches[0]將包含完整模式匹配到的文本,$matches[1] 將包含捕獲子組匹配到的文本。
對於第二個函數,matches是必填項,它是一個多維數組,作為輸出參數輸出所有匹配結果, 數組排序通過flags
指定。
一個簡單的demo來查看區別:
$pattern = '/[0-9]/'; $subject = 'afdddd3asfs4asf6asf1'; $m1= $m2 = []; preg_match($pattern, $subject, $m1); preg_match_all($pattern, $subject, $m2); show($m1); echo '<hr>'; show($m2);
結果:
Array ( [0] => 3 ) ---------------------- Array ( [0] => Array ( [0] => 3 [1] => 4 [2] => 6 [3] => 1 ) )
preg_replace 與 preg_filter
3、preg_replace ( $pattern, $replacement, $subject ) //執行一個正則表達式的搜索和替換
4、preg_filter ( $pattern, $replacement, $subject ) //執行一個正則表達式搜索和替換
參數說明:
pattern 要搜索的模式。可以使一個字符串或者字符串數組。可以使用PCRE修飾符
replacement 用於替換的字符串或者字符串數組。
subject 要進行搜索和替換的字符串或字符串數組。
區別:preg_filter 它僅僅返回(可能經過轉化)與目標匹配的結果,功能和preg_replace類似
返回值:如果subject
是一個數組,返回一個數組, 其他情況返回一個字符串。
如果沒有找到匹配或者發生了錯誤,當subject
是數組 時返回一個空數組,其他情況返回NULL
。
分別用3個簡單的demo來查看區別:
測試1:
$pattern = '/[0-9]/'; $subject = 'afdddd3asfs4asf6asf1'; $replacement = 'CHINA'; $str1 = preg_replace($pattern, $replacement, $subject); $str2 = preg_filter($pattern, $replacement, $subject); show($str1); echo '<hr>'; show($str2);
結果1:
afddddCHINAasfsCHINAasfCHINAasfCHINA
------------------------------------- afddddCHINAasfsCHINAasfCHINAasfCHINA
preg_replace 和 preg_filter都支持數組替換的,繼續看區別
測試2:
//$pattern = '/[0-9]/'; $pattern = [ '/[123]/', '/[4567]/', '/[890]/']; $subject = 'afdddd3asfs4asf9asf1'; //$replacement = 'CHINA'; $replacement = ['你', '好', '嗎']; $str1 = preg_replace($pattern, $replacement, $subject); $str2 = preg_filter($pattern, $replacement, $subject); show($str1); echo '<hr>'; show($str2);
結果2:
afdddd你asfs好asf嗎asf你 ------------------------------ afdddd你asfs好asf嗎asf你
貌似還是沒有區別和變化,繼續測試
測試3:
//$pattern = '/[0-9]/'; $pattern = [ '/[123]/', '/[4567]/', '/[890]/']; //$subject = 'afdddd3asfs4asf9asf1'; $subject = ['afddd', 'd3asf', 's4', 'as', '9as', 'f1']; //$replacement = 'CHINA'; $replacement = ['你', '好', '嗎']; $str1 = preg_replace($pattern, $replacement, $subject); $str2 = preg_filter($pattern, $replacement, $subject); show($str1); echo '<hr>'; show($str2);
結果3:
Array ( [0] => afddd [1] => d你asf [2] => s好 [3] => as [4] => 嗎as [5] => f你 ) -------------------- Array ( [1] => d你asf [2] => s好 [4] => 嗎as [5] => f你 )
由此可見,preg_replace 替換會把每一個數組中的元素都進行一次匹配替換,無論有沒有匹配成功,返回值中的元素都會保留,而preg_filter 只會保留匹配成功的元素;
5、preg_grep ( $pattern, array $input ) // 返回匹配模式的數組條目
只做匹配,不做替換,是preg_filter的簡版;
參數說明:
pattern 要搜索的模式,字符串形式
input 輸入數組
通過demo查看效果:
$pattern = '/[0-9]/'; $subject = ['afddd', 'd3asf', 's4', 'as', '9as', 'f1']; $str1 = preg_grep($pattern, $subject); show($str1);
結果:
Array ( [1] => d3asf [2] => s4 [4] => 9as [5] => f1 )
由此可見,preg_grep針對 subject 中的每一個元素都進行了匹配,且結果只會打印匹配到的數據;
6、preg_split ( $pattern, $subject ) // 通過一個正則表達式分隔字符串
說明:通過正則表達式匹配字符串,將字符串按正則拆分成數組,如果你不需要正則表達式功能,可以有更快(並且更簡單)的選擇比如 explode() 或 str_split()。如果沒有成功匹配,將會返回一個數組,包含了單個元素,即輸入的字符串。
參數說明:
pattern 用搜索的模式,字符串形式。
subject 輸入字符串
通過demo查看效果:
$pattern = '/[0-9]/'; $subject = '我在3上海5,你知道2的嗎?'; $str1 = preg_split($pattern, $subject); show($str1);
結果:
Array ( [0] => 我在 [1] => 上海 [2] => ,你知道 [3] => 的嗎? )
由此可見,通過正則表達式,凡是遇到0-9之間的阿拉伯數字都會切一刀拆分成數組;
7、preg_quote($str) //轉義正則表達式字符
說明:preg_quote()需要參數 str
並向其中 每個正則表達式語法中的字符前增加一個反斜線。 這通常用於你有一些運行時字符串 需要作為正則表達式進行匹配的時候。
正則表達式特殊字符有: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -
注意 / 不是正則表達式特殊字符
用demo示例效果:
$str = 'this is {abc} and [456]'; $str = preg_quote($str); show($str);
結果:
this is \{abc\} and \[456\]
由此可見,{} 花括號和 [] 方括號都是正則表達式的特殊符號,通過preg_quote 函數進行轉譯后多了\,因此進行正則匹配的時候會忽略該特殊符號;
二、正則表達式常用總結
1、都以preg_ 開頭
2、除 preg_quote 函數之外,第一個參數都是正則表達式
3、preg_match -------表單驗證等
4、preg_replace -----非法詞語過濾等
注:學習筆記