說明以下 PREG 模式有preg_filter;preg_replace;preg_grep;preg_quote;preg_match;preg_match_all;preg_split;preg_replace_callback_array
一、內部選項設置:
Internal option letters 內部選項字母
i for PCRE_CASELESS 輕巧
m for PCRE_MULTILINE 多線
s for PCRE_DOTALL 點球
x for PCRE_EXTENDED 擴展
U for PCRE_UNGREEDY 勇氣
X for PCRE_EXTRA 額外
J for PCRE_INFO_JCHANGED 信息已更改
二、程式範例:
1. preg_filter:執行正則表達式搜索並替換
$subject = array('1', 'a', '2', 'b', '3', 'A', 'B', '4');
$result = preg_filter('/\d/', '$0', $subject);
print_r($result);
結果:Array ( [0] => 1 [2] => 2 [4] => 3 [7] => 4 )
2. preg_replace 用法:執行正則表達式搜索並替換,preg_replace(模式,替代,字串);
用法一:
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${1}1,$3';
$text = preg_replace($pattern, $replacement, $string);
print_r($text);
指 $1=April $2=15 $3=2003 ${1}1=April1
結果:April1,2003
用法二:
$text_1 = preg_replace('/(\w+)/i', '${1}2', $string);
print_r($text_1);
結果:April2 152, 20032
用法三:如何刪除 script 之間的內容方法如下:
preg_replace("/<script.*>(.|\r|\n)*?<\\/script>/", "", $str);
注:preg_filter 與 preg_replace 差別:
preg_filter 沒找到會空值,preg_replace 沒找到會輸出原來的值。
$subject = array('1', '2', '3', 'a', 'b', 'A', 'B');
$pattern = array('/\d/', '/[a-z]/', '/[1a]/');
$replace = array('A:$0', 'B:$0', 'C:$0');
echo "</br>preg_filter===>\n";
print_r(preg_filter($pattern, $replace, $subject));
//結果:Array ( [0] => A:C:1 [1] => A:2 [2] => A:3 [3] => B:C:a [4] => B:b )
echo "</br>preg_replace===>\n";
print_r(preg_replace($pattern, $replace, $subject));
//結果:Array ( [0] => A:C:1 [1] => A:2 [2] => A:3 [3] => B:C:a [4] => B:b [5] => A [6] => B )
3. preg_grep 用法:返回與模式匹配的數組條目
$array=["123d.888","55a.555"];
$fl_array = preg_grep("/^(\d+)?\.\d+$/", $array);
print_r($fl_array);
4. preg_quote 用法:引用正則表達式字符
$keywords = '$50 , . g4/600';
$keywords = preg_quote($keywords, '/');
echo $keywords; // returns \$40 for a g3\/400
5. preg_match 用法:執行正則表達式匹配
$subject = "abcdef";
$pattern = '/def/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
//結果:Array ( [0] => Array ( [0] => def [1] => 3 ) )
6. preg_match_all 用法:執行全局正則表達式匹配
$out[0] 保存完整模式的所有匹配 $out[1]是子组的所有匹配
[^>]+指 > 的開始位置,+表示一次或多次, 後面 > 指的原來的 >
preg_match_all("|<[^>]+>(.*)</[^>]+>|U","<b>example: </b><div align=left>happy to tody</div>",$out,PREG_PATTERN_ORDER);
print_r($out);
/*結果:
Array ( [0] => Array ( [0] => example: [1] =>
happy to tody
) [1] => Array ( [0] => example: [1] => happy to tody ) )
*/
取得<div>到</div>之間的方法:
preg_match_all("|<[^>]+>(.*)</[^>]+>|U","<b>example: </b>
<div align=left>happy to tody</br></div>",$out,PREG_PATTERN_ORDER);
print_r($out);
取得兩個不同的 <div> 與 </div>之間的內容
preg_match_all("/<div [id|class]+.*?>(.*?)<\/div>/","<div id=\"a1\">example: </div><div class=\"a1\">happy to tody</div>",$out,PREG_SET_ORDER);
print_r($out);
/*結果:
Array ( [0] => Array ( [0] =>
example:
[1] => example: ) [1] => Array ( [0] =>
happy to tody
[1] => happy to tody ) )
*/
7. preg_split 用法:通過正則表達式拆解字串
空白加逗點拆解字串 => [\s,]
$keywords = preg_split("/[\s,]+/", "happy day, 123");
print_r($keywords);
//結果:Array ( [0] => happy [1] => day [2] => 123 )
8. preg_replace_callback:執行正則表達式搜索並使用回調進行替換
$text = "A 04/01/2004\n";
function next_year($t1){
return ($t1[1]+1);
}
echo preg_replace_callback("|(\d{2})|","next_year",$text);
//結果:A 5/2/213
9. preg_replace_callback_array:執行正則表達式搜索並使用回調進行替換,只能 PHP 7才能使用。
$subject = 'Tttttttt Ccccc';
preg_replace_callback_array(
[
'~[a]+~i' => function ($match) {
echo strlen($match[0]), ' matches for "a" found', PHP_EOL;
},
'~[b]+~i' => function ($match) {
echo strlen($match[0]), ' matches for "b" found', PHP_EOL;
}
],
$subject
);
資料來源:https://www.php.net/manual/en/ref.pcre.php
留言列表