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
|
<?php if (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 ...
随机推荐
- Sublime Text 3 配置分析与我的配置---小结
Sublime Text 3 配置解释(默认){// 设置主题文件"color_scheme": "Packages/Color Scheme – Default/Mon ...
- 微信小程序开发之模板
一.简介 WXML提供模板(template),可以在模板中定义代码片段,然后在不同的地方调用. 定义模板 使用name属性,作为模板的名字.然后在<template/>内定义代码片段,如 ...
- robotframework的学习笔记(十四)------学习Robot Framework必须掌握的库—-BuiltIn库
作为一门表格语言,为了保持简单的结构,RF没有像别的高级语言那样提供类似if else while等内置关键字来实现各种逻辑功能,而是提供给了用户BuiltIn库.如果用户想在测试用例中实现比较复杂的 ...
- HTML5图片上传本地预览
在开发 H5 应用的时候碰到一个问题,应用只需要一张小的缩略图,而用户用手机上传的确是一张大图,手机摄像机拍的图片好几 M,这可要浪费很多流量. 我们可以通过以下方式来解决. 获取图片 通过 File ...
- rpc之thrift
rpc之thrift 一.介绍 thrift是一个rpc(remove procedure call)框架,可以实现不同的语言(java.c++.js.python.ruby.c#等)之间的相互调用. ...
- iOS libyuv
libyuv是Google开源库,可用作图像数据格式的转换,比如视频流编解码时格式的转换,YUV数据转化RGB等 libyuv静态库 为了方便使用,已经将libyuv源代码打包成了iOS静态库,lib ...
- python之list
1.python列表 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推 序列都可以进行的操作包括索引,切片,加,乘, ...
- 从零开始开发一个简易的类vue-cli构建工具
代码地址:https://github.com/cheer4chai/webpack-learning 仿照vue-cli开发这个工具的目的是了解webpack的基本设置,以及vue-cli的工作原理 ...
- Visual Studio 中添加SQLite数据源
相关下载:https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki 在Visual Studio中要支持访问SQLi ...
- <The Art of Readable Code> 笔记二 (下)
第1章 封装信息到名字 (Packing information into names) 4 附加额外信息 1) encode value type 对于某些变量,附加额外的信息可以让人更好的理 ...