php中preg正則函數使用,phppreg函數使用
1.preg_match和preg_match_all的區別
preg_match和 preg_match_all區別是preg_match只匹配一次。而preg_match_all全部匹配,直到字串結束。例:
<?php//註:正則 /a.+?e/ 是非貪婪模式(因為量詞‘+’後面加上了‘?’),如果使用 /a.+?e/U 則變回了貪婪模式preg_match("/a.+?e/","abcdefgabcdefgabcdefg",$out1);preg_match_all("/a.+?e/","abcdefgabcdefgabcdefg",$out2);var_dump($out1);var_dump($out2);/*輸出:array (size=1) 0 => string 'abcde' (length=5)array (size=1) 0 => array (size=3) 0 => string 'abcde' (length=5) 1 => string 'abcde' (length=5) 2 => string 'abcde' (length=5) */?>2.貪婪模式和非貪婪模式的區別
如:String str="abcaxc";
Patter p="ab*c";
貪婪匹配:Regex一般趨向於最大長度匹配,也就是所謂的貪婪匹配。如上面使用模式p匹配字串str,結果就是匹配到:abcaxc(ab*c)。
非貪婪匹配:就是匹配到結果就好,較少的匹配字元。如上面使用模式p匹配字串str,結果就是匹配到:abc(ab*c)。
例:
<?php$str = "http://www.baidu/.com?url=www.sina.com/";preg_match("/http:(.*)com/", $str, $matches1); //貪婪匹配var_dump($matches1);preg_match("/http:(.*?)com/", $str, $matches2); //非貪婪匹配(量詞'*'後面跟上了'?')var_dump($matches2);/*array (size=2) 0 => string 'http://www.baidu/.com?url=www.sina.com' (length=38) 1 => string '//www.baidu/.com?url=www.sina.' (length=30)array (size=2) 0 => string 'http://www.baidu/.com' (length=21) 1 => string '//www.baidu/.' (length=13) */?>3.preg_match_all參數PREG_PATTERN_ORDER(預設)和PREG_SET_ORDER的區別
<?phpecho('PREG_PATTERN_ORDER');preg_match_all("|<[^>]+>(.*)</[^>]+>|U", "<b>start: </b><b>this is a test</b><b>end</b>", $out1);var_dump($out1);echo('PREG_SET_ORDER');preg_match_all("|<[^>]+>(.*)</[^>]+>|U", "<b>start: </b><b>this is a test</b><b>end</b>", $out2, PREG_SET_ORDER);var_dump($out2);/*PREG_PATTERN_ORDERarray (size=2) 0 => array (size=3) 0 => string '<b>start: </b>' (length=14) 1 => string '<b>this is a test</b>' (length=21) 2 => string '<b>end</b>' (length=10) 1 => array (size=3) 0 => string 'start: ' (length=7) 1 => string 'this is a test' (length=14) 2 => string 'end' (length=3)PREG_SET_ORDERarray (size=3) 0 => array (size=2) 0 => string '<b>start: </b>' (length=14) 1 => string 'start: ' (length=7) 1 => array (size=2) 0 => string '<b>this is a test</b>' (length=21) 1 => string 'this is a test' (length=14) 2 => array (size=2) 0 => string '<b>end</b>' (length=10) 1 => string 'end' (length=3) */?>
延伸閱讀:preg_match_all使用執行個體
怎運用PHP函數preg_match_all測試Regex效果
PHP函數preg_match_all代碼執行個體:php自學網2< /div< div id="biuuu_3"php自學網3< /div'; PHP函數preg_match_all執行個體要求:分別將每一個DIV元素的ID和內容取出,如biuuu,biuuu_2,biuuu_3,php自學網,php自學網2和php自學網3(一些常用的抓站方法就是這樣匹配的) 分析:字串是一個簡單的HTML元素,每一個DIV元素對應該一個ID和內容,並且是獨立的,首先考慮如何取出一個DIV內的ID值和內容, 如:php自學網,然後匹配其它類似的元素。一個DIV中需要取出兩個值,也就是兩個匹配的運算式,第一個運算式用於匹配ID值(biuuu),第二個表 達式用於匹配ID的內容(php自學網),Regex常用的運算式使用小括弧,那麼前面的元素將會變成如下形式: < div id="(biuuu)"(php自學網)< /div < div id="(運算式1)"(運算式2)< /div 運算式1:[a-zA-Z0-9_]+ (表示匹配大小寫字母,數字和底線) 運算式2:[^<]+ (表示不匹配<和字元) 這樣,PHP函數preg_match_all需要匹配的子運算式就實現了,但是還要需要匹配一個的運算式,方法如下: 運算式:/ '"(運算式1)"'(運算式2)/ 注意其中的雙引號"和/需要使用逸出字元轉義,然後把前面兩個運算式放進去,如下: '"([a-z0-9_]+)"'/< div id="([a-z0-9_]+)"([^<]+)< /div/ 這樣就實現一個匹配每一個DIV元素ID值和內容的Regex,然後使用preg_match_all函數測試如下: $html = '< div id="biuuu"php自學網< /div< div id="biuuu_2"php自學網2< /div< div id="biuuu_3"php自學網3< /div'; preg_match_all('/< divsiddivsid="([a-z0-9_]+)"([^<]+)< /div/',$html,$result); var_dump($result); PHP函數preg_match_all樣本結果: array(3) { [0]= array(3) { [0]= string(30) "php自學網" [1]= string(33) "php自學網2" [2]= string(33) "php自學網3" } [1]= array(3) { [0]= string(5) "biuuu" [1]= string(7) "biuuu_2" [2]= string(7) "biuuu_3&......餘下全文>>
php中preg_replace函數的正則問題
試編寫代碼如下:
<?php
$s1 = "<?php \$var['a']; ?>";
$s2 = "<?php \$var['a']['bbb']; ?>";
$pa = '/<\?php +\$([_a-zA-Z0-9]+)(\[\'[a-zA-Z0-9]+\'\]+)(\[\'[a-zA-Z0-9]+\'\])*; ?\?>/si';
echo preg_replace_callback($pa, 'doReplace', $s2);
function doReplace($matches)
{
$resut = '<?php $ehs->get(' .$matches[1]. ')->value';
for($i=2;$i<count($matches);$i++)
{
$resut .= $matches[$i];
}
return $resut;
}
?>
//簡便代碼
<?php
$s1 = "<?php \$var['a']; ?>";
$s2 = "<?php \$var['a']['bbb']['cccc']; ?>";
$pa = '/<\?php +\$([_a-zA-Z0-9]+)([\'\S\']+); ?\?>/si';
echo preg_replace_callback($pa, 'doReplace', $s2);
function doReplace($matches)
{
return '<?php $ehs->get(' .$matches[1]. ')->value'. $matches[2];
}
?>