PHP中運用Regex
1、函數
1)preg_match ―進行Regex匹配,相容perl的正則文法
int preg_match (string$pattern ,string $subject [,array$matches [,int $flags ]] )
-
加上標誌 PREG_OFFSET_CAPTURE 後,變成反向匹配
2)int ereg (string$pattern ,string $string [,array&$regs ])
註:preg_match函數比 ereg() 快。
3)preg_grep ― 返回與模式比對的數組單元
array preg_grep ( string $pattern , array $input [, int $flags ])
preg_grep() 返回一個數組,其中包括了 input 數組中與給定的 pattern 模式相匹配的單元。
同樣加入標誌 PREG_GREP_INVERT ,則變成反向匹配
(與正則文法中的?!對等)
如
$subject = "4|43|WINWORD.EXE|C:\DOCUME~1\iloveyou\LOCALS~1\Temp\~DF39E9.tmp||eew|1300763364|";
$pattern = '/\.(?!EXE|tmp).{3}/';
preg_match($pattern, $subject, $matches);
print_r($matches);
?>
2、如何讓Regex不區分大小寫
sql_regcase ― 產生用於不區分大小的匹配的Regex
$fl_array = preg_grep (sql_regcase("/^(ASSZs)/"), $array);
3、如何根據檔案尾碼過濾檔案名稱
$logs =preg_grep(sql_regcase('/^.*?\.(tmp|ini|pip).*?/'),$logstring,PREG_GREP_INVERT);
過濾掉尾碼名為tmp|ini|txt的檔案名稱,且不區分大小寫。
來自東子哥的Blog