PHP PCRE 函数如何高效处理正则表达式实例?

2026-04-02 05:101阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1065个文字,预计阅读时间需要5分钟。

PHP PCRE 函数如何高效处理正则表达式实例?

本例介绍了PHP正则表达式处理函数。在特定业务场景中,可能需要匹配或提取关键信息,例如匹配网页中的链接。以下是一个简单示例:

php

$=<<

$links=extractLinks($);print_r($links);?>

输出结果:Array( [0]=> https://www.example.com [1]=> https://www.example.org)

本文实例讲述了PHP正则表达式处理函数。分享给大家供大家参考,具体如下:

有时候在一些特定的业务场景中需要匹配,或者提取一些关键的信息,例如匹配网页中的一些链接,

提取一些数据时,可能会用到正则匹配。

PHP PCRE 函数如何高效处理正则表达式实例?

下面介绍一下php中的一些常用的正则处理函数。

一、preg_replace($pattern,$replacement,$subject)

执行一个正则表达式的搜索和替换。

<?php echo "<pre>"; $str = "12,34:56;784;35,67:897:65"; //要求将上面的:,;都换成空格 print_r(preg_replace("/[,;:]/"," ",$str)); ?>

输出

12 34 56 784 35 67 897 65

二、preg_match($pattern,$subject,&$matches)

执行匹配正则表达式

<?php echo "<pre>"; $str = "<a href=\"www.baidu.com\">团购商品</a>"; //匹配出链接地址 preg_match("/<a href=\"(.*?)\">.*?<\/a>/",$str,$res); print_r($res); ?>

输出

Array
(
[0] => 团购商品
[1] => www.baidu.com
)

三、preg_match_all($pattern,$subject,&$matches)

执行一个全局正则表达式匹配

<?php echo "<pre>"; $str=<<<EOF <div> <a href="index.php" rel="external nofollow" >首页</a> <a href="category.php?id=3" rel="external nofollow" >GSM手机</a> <a href="category.php?id=4" rel="external nofollow" >双模手机</a> <a href="category.php?id=6" rel="external nofollow" >手机配件</a> </div> EOF; //使用全局正则匹配 preg_match_all("/<a href=\"(.*?)\">(.*?)<\/a>/s",$str,$res); print_r($res); ?>

输出

Array
(
[0] => Array
(
[0] => 首页
[1] => GSM手机
[2] => 双模手机
[3] => 手机配件
)
[1] => Array
(
[0] => index.php
[1] => category.php?id=3
[2] => category.php?id=4
[3] => category.php?id=6
)
[2] => Array
(
[0] => 首页
[1] => GSM手机
[2] => 双模手机
[3] => 手机配件
)
)

四、preg_split($pattern,$subject)

通过一个正则表达式分隔字符串

<?php echo "<pre>"; $str = "12,34:56;784;35,67:897:65"; //分隔字符串 $arr = preg_split("/[,;:]/",$str); print_r($arr); ?>

输出

Array
(
[0] => 12
[1] => 34
[2] => 56
[3] => 784
[4] => 35
[5] => 67
[6] => 897
[7] => 65
)

五、preg_quote($str)

转义正则表达式字符

正则表达式特殊字符有:. \ + * ? [ ^ ] $ ( ) { } = ! < > : -

<?php echo "<pre>"; echo preg_quote("(abc){10}");//在每个正则表达式语法的字符前增加一个反斜杠 ?>

输出

\(abc\)\{10\}

六、子存储

<?php echo "<pre>"; //子存储使用 $date="[2012-08-09],[2012,09-19],[2011/08,09],[2012/10/09],[2013,08,01]"; //将上面字串中合法的日期匹配出来 preg_match_all("/\[[0-9]{4}([\-,\/])[0-9]{2}\\1[0-9]{2}\]/",$date,$a); print_r($a); ?>

输出

Array
(
[0] => Array
(
[0] => [2012-08-09]
[1] => [2012/10/09]
[2] => [2013,08,01]
)
[1] => Array
(
[0] => -
[1] => /
[2] => ,
)
)

详细版请参考://www.jb51.net/article/160947.htm

PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

JavaScript正则表达式在线测试工具:
tools.jb51.net/regex/javascript

正则表达式在线生成工具:
tools.jb51.net/regex/create_reg

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php正则表达式用法总结》、《php程序设计安全教程》、《php安全过滤技巧总结》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《php字符串(string)用法总结》及《php+mysql数据库操作入门教程》

希望本文所述对大家PHP程序设计有所帮助。

本文共计1065个文字,预计阅读时间需要5分钟。

PHP PCRE 函数如何高效处理正则表达式实例?

本例介绍了PHP正则表达式处理函数。在特定业务场景中,可能需要匹配或提取关键信息,例如匹配网页中的链接。以下是一个简单示例:

php

$=<<

$links=extractLinks($);print_r($links);?>

输出结果:Array( [0]=> https://www.example.com [1]=> https://www.example.org)

本文实例讲述了PHP正则表达式处理函数。分享给大家供大家参考,具体如下:

有时候在一些特定的业务场景中需要匹配,或者提取一些关键的信息,例如匹配网页中的一些链接,

提取一些数据时,可能会用到正则匹配。

PHP PCRE 函数如何高效处理正则表达式实例?

下面介绍一下php中的一些常用的正则处理函数。

一、preg_replace($pattern,$replacement,$subject)

执行一个正则表达式的搜索和替换。

<?php echo "<pre>"; $str = "12,34:56;784;35,67:897:65"; //要求将上面的:,;都换成空格 print_r(preg_replace("/[,;:]/"," ",$str)); ?>

输出

12 34 56 784 35 67 897 65

二、preg_match($pattern,$subject,&$matches)

执行匹配正则表达式

<?php echo "<pre>"; $str = "<a href=\"www.baidu.com\">团购商品</a>"; //匹配出链接地址 preg_match("/<a href=\"(.*?)\">.*?<\/a>/",$str,$res); print_r($res); ?>

输出

Array
(
[0] => 团购商品
[1] => www.baidu.com
)

三、preg_match_all($pattern,$subject,&$matches)

执行一个全局正则表达式匹配

<?php echo "<pre>"; $str=<<<EOF <div> <a href="index.php" rel="external nofollow" >首页</a> <a href="category.php?id=3" rel="external nofollow" >GSM手机</a> <a href="category.php?id=4" rel="external nofollow" >双模手机</a> <a href="category.php?id=6" rel="external nofollow" >手机配件</a> </div> EOF; //使用全局正则匹配 preg_match_all("/<a href=\"(.*?)\">(.*?)<\/a>/s",$str,$res); print_r($res); ?>

输出

Array
(
[0] => Array
(
[0] => 首页
[1] => GSM手机
[2] => 双模手机
[3] => 手机配件
)
[1] => Array
(
[0] => index.php
[1] => category.php?id=3
[2] => category.php?id=4
[3] => category.php?id=6
)
[2] => Array
(
[0] => 首页
[1] => GSM手机
[2] => 双模手机
[3] => 手机配件
)
)

四、preg_split($pattern,$subject)

通过一个正则表达式分隔字符串

<?php echo "<pre>"; $str = "12,34:56;784;35,67:897:65"; //分隔字符串 $arr = preg_split("/[,;:]/",$str); print_r($arr); ?>

输出

Array
(
[0] => 12
[1] => 34
[2] => 56
[3] => 784
[4] => 35
[5] => 67
[6] => 897
[7] => 65
)

五、preg_quote($str)

转义正则表达式字符

正则表达式特殊字符有:. \ + * ? [ ^ ] $ ( ) { } = ! < > : -

<?php echo "<pre>"; echo preg_quote("(abc){10}");//在每个正则表达式语法的字符前增加一个反斜杠 ?>

输出

\(abc\)\{10\}

六、子存储

<?php echo "<pre>"; //子存储使用 $date="[2012-08-09],[2012,09-19],[2011/08,09],[2012/10/09],[2013,08,01]"; //将上面字串中合法的日期匹配出来 preg_match_all("/\[[0-9]{4}([\-,\/])[0-9]{2}\\1[0-9]{2}\]/",$date,$a); print_r($a); ?>

输出

Array
(
[0] => Array
(
[0] => [2012-08-09]
[1] => [2012/10/09]
[2] => [2013,08,01]
)
[1] => Array
(
[0] => -
[1] => /
[2] => ,
)
)

详细版请参考://www.jb51.net/article/160947.htm

PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

JavaScript正则表达式在线测试工具:
tools.jb51.net/regex/javascript

正则表达式在线生成工具:
tools.jb51.net/regex/create_reg

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php正则表达式用法总结》、《php程序设计安全教程》、《php安全过滤技巧总结》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《php字符串(string)用法总结》及《php+mysql数据库操作入门教程》

希望本文所述对大家PHP程序设计有所帮助。