php preg_match_all函数笔记
来源:互联网 发布:ubantu安装mysql数据库 编辑:程序博客网 时间:2024/05/21 16:18
preg_match_all进行全局正则表达式匹配
int preg_match_all(string pattern,string subject,array matches [,int flags)
在 subject 中搜索所有与 pattern 给出的正则表达式匹配的内容并将结果以 flags 指定的顺序放到 matches 中,搜索到第一个匹配项之后,接下来的搜索从上一个匹配项末尾开始
如果使用PREG_PATTERN_ORDER
对结果排序使 $matches[0] 为全部模式匹配的数组,$matches[1] 为第一个括号中的子模式所匹配的字符串组成的数组,以此类推。(即$matches[0] [0]为全部模式匹配中的每一项,$matches[0] [1]为全部模式匹配中的第二项,$matches[1] [0]为匹配每一个括号中的第一项,$matches[1] [0]为匹配每一个括号中的第二项)
<?php
preg_match_all ("|<[^>]+>(.*)</[^>]+>|U","<b>example: </b><div align=left>this is a test</div>",$out, PREG_PATTERN_ORDER);
print $out[0][0].", ".$out[0][1]."\n";
print $out[1][0].", ".$out[1][1]."\n";
输出:
<b>example: </b>, <div align=left>this is a test</div>
example: , this is a test
因此,$out[0] 包含匹配整个模式的字符串,$out[1] 包含一对 HTML 标记之间的字符串。
如果使用PREG_SET_ORDER
对结果排序使 $matches[0] 为第一组匹配项的数组,$matches[1] 为第二组匹配项的数组,以此类推。(即$matches[0] [0]为第一组匹配项中完整匹配的字符串,$matches[0] [1]为第一组匹配中完整匹配第一个括号中的字符串)
<?phppreg_match_all ("|<[^>]+>(.*)</[^>]+>|U","<b>example: </b><div align=left>this is a test</div>",$out, PREG_SET_ORDER);
print $out[0][0].", ".$out[0][1]."\n";
print $out[1][0].", ".$out[1][1]."\n";
输出:
<b>example: </b>, example:
<div align=left>this is a test</div>, this is a test
本例中,$matches[0] 是第一组匹配结果,$matches[0][0] 包含匹配整个模式的文本,$matches[0][1] 包含匹配第一个子模式的文本,以此类推。同样,$matches[1] 是第二组匹配结果,等等。
- php preg_match_all函数笔记
- php preg_match()与preg_match_all()函数
- PHP中preg_match_all函数正则匹配详解
- PHP函数preg_match_all正则表达式的简单应用
- PHP函数preg_match_all正则表达式的简单应用
- PHP 正则表达式匹配 preg_match 与 preg_match_all 函数
- php小经验:解析preg_match与preg_match_all 函数
- PHP 正则preg_match 与 preg_match_all 函数以及匹配中文
- PHP 正则表达式匹配 preg_match 与 preg_match_all 函数
- PHP 正则表达式匹配 preg_match 与 preg_match_all 函数
- php preg_match_all() 用法
- PHP preg_match_all详解
- preg_match_all 函数 使用
- preg_match 与 preg_match_all 函数
- preg_match_all函数详解
- preg_match与preg_match_all 函数
- PHP 的 preg_match 和 preg_match_all
- preg_match_all
- 51nod 1135 原根(求素数最小原根)
- 软件工程(十九)
- HDUOJ_2567_寻梦
- 生产者和消费者案例
- JDBC连接MySQL步骤
- php preg_match_all函数笔记
- C++作业6
- JS封装简单后代选择器
- C++第六次作业
- Activity的onCreate中启动新的Activity时生命周期
- EffecitveJava 第二章
- POJ 1845 Sumdiv(逆元、分治)【真心好题啊=_=】
- LeetCode 044 Wildcard Matching
- 【Ionic】Error: failed to fetch platform android