PHP 正则表达式匹配函数 preg_match 与 preg_match_all
preg_match()
preg_match() 函数用于进行正则表达式匹配,成功返回 1 ,否则返回 0 。
语法:
|
1
|
int preg_match( string pattern, string subject [, array matches ] ) |
参数说明:
| 参数 | 说明 |
|---|---|
| pattern | 正则表达式 |
| subject | 需要匹配检索的对象 |
| matches | 可选,存储匹配结果的数组, $matches[0] 将包含与整个模式匹配的文本,$matches[1] 将包含与第一个捕获的括号中的子模式所匹配的文本,以此类推 |
例子 1:
|
1
2
3
4
5
6
7
8
9
|
<?phpif (preg_match("/php/i", "PHP is the web scripting language of choice.", $matches)){ print "A match was found:" . $matches[0];}else{ print "A match was not found.";} |
输出:
|
1
|
A match was found:PHP |
在该例子中,由于使用了 i 修正符,因此会不区分大小写去文本中匹配 php 。
注意:
preg_match() 第一次匹配成功后就会停止匹配,如果要实现全部结果的匹配,即搜索到subject结尾处,则需使用 preg_match_all() 函数。
例子 2 ,从一个 URL 中取得主机域名 :
|
1
2
3
4
5
6
7
8
|
<?php// 从 URL 中取得主机名preg_match("/^(http:\/\/)?([^\/]+)/i","http://blog.snsgou.com/index.php", $matches);$host = $matches[2];// 从主机名中取得后面两段preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);echo "域名为:{$matches[0]}"; |
输出:
|
1
|
域名为:snsgou.com |
preg_match_all()
preg_match_all() 函数用于进行正则表达式全局匹配,成功返回整个模式匹配的次数(可能为零),如果出错返回 FALSE 。
语法:
|
1
|
int preg_match_all( string pattern, string subject, array matches [, int flags ] ) |
参数说明:
| 参数 | 说明 |
|---|---|
| pattern | 正则表达式 |
| subject | 需要匹配检索的对象 |
| matches | 存储匹配结果的数组 |
| flags |
可选,指定匹配结果放入 matches 中的顺序,可供选择的标记有:
|
下面的例子演示了将文本中所有 <pre></pre> 标签内的关键字(php)显示为红色。
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php$str = "<pre>学习php是一件快乐的事。</pre><pre>所有的phper需要共同努力!</pre>";$kw = "php";preg_match_all('/<pre>([\s\S]*?)<\/pre>/', $str, $mat);for ($i = 0; $i < count($mat[0]); $i++){ $mat[0][$i] = $mat[1][$i]; $mat[0][$i] = str_replace($kw, '<span style="color:#ff0000">' . $kw . '</span>', $mat[0][$i]); $str = str_replace($mat[1][$i], $mat[0][$i], $str);}echo $str;?> |
输出效果:

简化一下:
|
1
2
3
4
5
|
<?php$str = "<pre>学习php是一件快乐的事。</pre><pre>所有的phper需要共同努力!</pre>";preg_match_all('/<pre>([\s\S]*?)<\/pre>/', $str, $matches);print_r($matches);?> |
输出:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
Array( [0] => Array ( [0] => <pre>学习php是一件快乐的事。</pre> [1] => <pre>所有的phper需要共同努力!</pre> ) [1] => Array ( [0] => 学习php是一件快乐的事。 [1] => 所有的phper需要共同努力! )) |
参考:
http://php.net/manual/zh/function.preg-match-all.php
PHP 正则表达式匹配函数 preg_match 与 preg_match_all的更多相关文章
- php正则表达式匹配函数
<?php function show($var=null){ if(empty($var)) { echo 'null'; }else if(is_array($var) || is_obj ...
- php 函数preg_match、preg_match_all ,以及正则表达式规则
<?php $str = 'php is the best language phhhhp is'; $part = '/ph{1,}p/'; echo preg_match($part, $s ...
- php中函数preg_match或preg_match_all 第三个参数$match的解释
理解自:http://www.cnblogs.com/vicenteforever/articles/1623137.html php手册中是这样解释的 matches 如果提供了参数matches, ...
- klee 测试一个简单的正则表达式匹配函数
函数源代码位于 klee源码 的examples/regexp文件夹下面:c程雪源码文件名为 Regexp.c First Step: 使用clang编译器将c源代码转化为llvm位码形式.如果你的 ...
- PHP 正则表达式常用函数使用小结
在PHP中有两套正则表达式函数库.一套是由PCRE(Perl Compatible Regular Expression)库提供的.PCRE库使用和Perl相同的语法规则实现了正则表达式的模式匹配,其 ...
- oracle 正则表达式 匹配
oracle 正则表达式 在实际应用中,想排除带有中文的字段值: select h.froomnumber from t_broker_house h where REGEXP_LIKE(froomn ...
- PHP 正则表达式匹配 preg_match 与 preg_match_all 函数
--http://www.5idev.com/p-php_preg_match.shtml 正则表达式在 PHP 中的应用 在 PHP 应用中,正则表达式主要用于: 正则匹配:根据正则表达式匹配相应的 ...
- preg_match 与 preg_match_all 函数示例详解
正则表达式在 PHP 中的应用 在 PHP 应用中,正则表达式主要用于: 正则匹配:根据正则表达式匹配相应的内容 正则替换:根据正则表达式匹配内容并替换 正则分割:根据正则表达式分割字符串 在 PHP ...
- 使用正则表达式匹配JS函数代码
使用正则表达式匹配JS函数代码 String someFunction="init"; Pattern regex = Pattern.compile("function ...
随机推荐
- django xdmin使用
我们来看看我们原先django给我们自带的admin后台是什么样子的呢 有人说,你的界面怎么那么丑,我说这个还叫丑吗,他说丑,我说你来,我看看你的,上图 看到登录界面后,我说别看了,我去修改,修改,我 ...
- IIS加载JSON文件 错误 404
问题描述 在发布项目的时候,有一些文件是json文件,在网页中进行加载,但是在IIS7发布的时候,json文件居然是404,无法找到,在URL上输入地址也一样. 错误原因 IIS内部机制,不支持直接访 ...
- Kettle中忽略错误行继续执行
在kettle执行的过程中,如果遇到错误,kettle会停止运行.在某些时候,并不希望kettle停止运行,所以就要处理下这些错误行. 例如这两天发现在一个转换中,总数出现一些不规则数据,这些数据一出 ...
- linux svn up 中文显示乱码解决办法
vi /etc/sysconfig/i18n #LANG="en_US.UTF-8" #LANG=zh_CN.GB18030 #LC_ALL=zh_CN.GB18030 #SYSF ...
- Robot Framework学习笔记(五)------Collections 库
Collections 库同样为 Robot Framework 标准类库,它所提供的关键字主要用于列表.索引.字典的处理. 1.添加类 在使用之前需要在测试套件(项目)中添加 2.创建字典 字典也是 ...
- CSS3 Media Queries 特性的妙用
第一招: 在网页中,pixel与point比值称为 device-pixel-ratio,普通设备都是1,iPhone 4是2,有些Android机型是1.5. 那么-webkit-min-devic ...
- Ubuntu 安装Appium
1.安装node apt-get install node.js 2.安装npm apt-get install npm 3.安装cnpm npm install -g cnpm 创建链接:ln -s ...
- python并发编程之多进程
一同步与异步 同步执行:一个进程在执行任务时,另一个进程必须等待执行完毕,才能继续执行 异步执行:一个进程在执行任务时,另一个进程无需等待其执行完毕就可以执行,当有消息返回时,系统会提醒后者进行处理, ...
- python3之socket&socketserver网络编程
1.套接字与套接模块 套接字是为特定网络协议(例如TCP/IP,ICMP/IP,UDP/IP等)套件对上的网络应用程序提供者提供当前可移植标准的对象.它们允许程序接受并进行连接,如发送和接受数据.为了 ...
- 0.python class
http://pythonprogramminglanguage.com/ 什么是python? python是一款让你工作比起用其他语言更快的编程语言.老练的程序员用其他的语言会比用python更顺 ...