PHP中应用正则表达式

WBOY
Release: 2016-06-13 12:50:45
Original
714 people have browsed it

PHP中运用正则表达式

1、函数

1)preg_match ―进行正则表达式匹配,兼容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、如何让正则表达式不区分大小写

sql_regcase ― 产生用于不区分大小的匹配的正则表达式

$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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!