PHP的 preg_match_all
语法:int preg_match_all ( string pattern, string subject, array &matches [, int flags] )
这个函数的返回值是整个pattern匹配到的结果数量。
matches(注意这里是引用)是一个二维数组,matches[0] 是整个pattern匹配的结果。matches[1] 是第一个子模式(正则表达式中的子模式,就是圆括号括起来的部分)匹配的结果,matches[2]是 第二个子模式的匹配结果,依次推matches[N]是 第N个子模式的匹配结果。
说明:有N个括号,就对应有N个子模式,子模式的元素就是括号中匹配到的内容。子模式中元素的个数=正则匹配到的结果的个数。
举例:
<?php
$pattern = "/<[^>]+>(.*)<\/[^>]+>/U";
$subject = "<b>example:</b><div align = left>this is a test</div>";
preg_match_all($pattern,$subject,$out);
print_r($out);
?>
结果:

$pattern 正则在字符串中匹配到了2个满足条件的结果,分别是
匹配结果1:
<b>example:</b> 匹配结果2:
<div align = left>this is a test</div>
所以每隔大数组里面的元素个数都是2,并且元素之间都是对应的(通过数组的索引键)
注意:这个结果的形式,array[0]是整个pattern匹配到的结果,array[1]是pattern中子模式 (.*) 所匹配到的结果。但是array[1]匹配的结果是和array[0]中的结果相对应
PHP的 preg_match_all的更多相关文章
- php_match/preg_match_all 默认有字符串长度限制
php_match/preg_match_all 默认有字符串长度限制:52500(或许你的服务器环境是更长,或者更短),当字符串长度大于52500,只能匹配到52500数据,超出的部分会被系统自己截 ...
- php 函数preg_match、preg_match_all ,以及正则表达式规则
<?php $str = 'php is the best language phhhhp is'; $part = '/ph{1,}p/'; echo preg_match($part, $s ...
- preg_match_all正则表达式的基本使用
了解正则表达式之前,须要掌握一些常用的正则表达式的基础知识,这些如果能记得最好记得,记不住须要用的时候能查到就行,就多个特殊字符,所以说正则表达式玩的就是特殊,具体大家可以查看更加细致的说明. pre ...
- php preg_match_all 和 str_replace 替换图片链接
需求:在 a 网站的后台添加一片文章,需要同步到 b 网站(两个网站数据库在同一台服务器). 思路是添加文章时,除了往 b 网站的数据库中添加数据外,再往 a 网站的数据库中添加数据. a 添加文章的 ...
- preg_match_all
<meta charset="utf8"> <?php set_time_limit (0); for($i=1;$i<34;$i++){ $url = & ...
- preg_match_all, preg_match
int preg_match(string $pattern, string $subject[, $arr][, int $flags]);$pattern 正则表达式$subject: 要搜索的字 ...
- PHP 正则表达式匹配 preg_match 与 preg_match_all 函数
--http://www.5idev.com/p-php_preg_match.shtml 正则表达式在 PHP 中的应用 在 PHP 应用中,正则表达式主要用于: 正则匹配:根据正则表达式匹配相应的 ...
- php preg_match 和preg_match_all
$h3=trim('[我要变强][主流程]整体功能测试点');$matches=array();preg_match_all('/[(.+?)]/', $h3,$matches); print_r($ ...
- javascript 正则匹配 提取所有 preg_match_all matchAll方法
javascript 提取全部的的方法.javascript中没有matchAll这种方法. 用while来实现类似 PHP 中的preg_match_all() :(by default7#zbph ...
- 学习PHP函数:preg_match_all
<?php $str = '10.10.10.10, 10.10.10.11'; preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', ...
随机推荐
- Yarn 踩坑 : ERROR: Cannot find configuration directory "/xxxx/xxxx/xxxxx/hadoop-x.x.x/conf"
解决:在 yarn-env.sh 中,指定 YARN_CONF_DIR 为 hadoop 目录的 /etc/hadoop 在yarn-env.sh 中,配置: export YARN_CONF_DIR ...
- hdu 1895 Sum Zero hash
Sum Zero Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Proble ...
- 拼接字符串,生成tree格式的JSON数组
之前做的执法文书的工作,现在需要从C#版本移植到网页版,从Thrift接口获取数据,加载到对应的控件中 之前用的easyui的Tree插件,通过<ul><li><span ...
- ckeditor5 安装高亮,颜色插件
ckeditor5 安装高亮插件 1.准备 git clone -b stable https://github.com/ckeditor/ckeditor5-build-classic.git cd ...
- 深入理解Plasma(一)Plasma 框架
这一系列文章将围绕以太坊的二层扩容框架,介绍其基本运行原理,具体操作细节,安全性讨论以及未来研究方向等.本篇文章作为开篇,主要目的是理解 Plasma 框架. Plasma 作为以太坊的二层扩容框架, ...
- 前端页面调用Spring boot接口发生的跨域问题
最近要重构一个基于spring boot的后端API服务,需要再本地测试.在本地测试时,运行在本地的前端页面发送一个ajax请求访问后端API,然后浏览器报错blocked CORS policy. ...
- 【转】 SQL - 生成指定范围内的随机数
DECLARE @Result INT DECLARE @Upper INT DECLARE @Lower INT SET @Lower = 1 SET @Upper = 10 SELECT @Res ...
- 在Tomcat中部署Web项目的操作方法,maven项目在Tomcat里登录首页报404
maven项目在Tomcat里登录首页报404, 解决:编辑conf/server.xml进行配置<Host>里的<Context>标签里的path. <Context ...
- 微服务-开发框架之springboot by 大雄daysn
目录 一.关于springboot 二.springboot的实践 2.1发布一个rest的api 2.2端点 2.3健康检查 2.4远程监控 一.关于springboot 由来:spring1.0- ...
- python全栈开发笔记---------字符串格式化
字符串格式化 %s 可以接收任何值, %d只能接收整形 .其他类型报错 msg ='i am %s my body' %'ales' print(msg) #i am ales my body msg ...