preg_match_all
(PHP 4, PHP 5, PHP 7)
preg_match_all - 执行全局正则表达式匹配
描述
int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )
在主题中搜索模式中给定的正则表达式的所有匹配项,并按照标志指定的顺序将它们放入匹配项中。
在找到第一次匹配后,从上次匹配结束后继续进行后续搜索。
参数
pattern
作为字符串搜索的模式。
subject
输入字符串。
matches
根据标志排列的多维数组中的所有匹配数组。
flags
可以是以下标志的组合(请注意,将PREG_PATTERN_ORDER和PREG_SET_ORDER一起使用是没有意义的):
PREG_PATTERN_ORDER
对结果排序以便$ matches0是完整模式匹配的数组,$ matches1是由第一个加括号的子模式匹配的字符串数组,等等。
<?php
preg_match_all("|<[^>]+>(.*)</[^>]+>|U",
????"<b>example:?</b><div?align=left>this?is?a?test</div>",
????$out,?PREG_PATTERN_ORDER);
echo?$out[0][0]?.?",?"?.?$out[0][1]?.?"\n";
echo?$out[1][0]?.?",?"?.?$out[1][1]?.?"\n";
?>
上面的例子将输出:
<b>example: </b>, <div align=left>this is a test</div>
example: , this is a test
因此,$ out0包含匹配完整模式的字符串数组,而$ out1包含由标签封装的字符串数组。
如果模式包含命名的子模式,则$匹配另外包含具有子模式名称的键的条目。
如果模式包含重复的命名子模式,则只有最右边的子模式存储在$ matchesNAME中。
<?php
preg_match_all(
????'/(?J)(?<match>foo)|(?<match>bar)/',
????'foo?bar',
????$matches,
????PREG_PATTERN_ORDER
);
print_r($matches['match']);
?>
上面的例子将输出:
Array
(
[0] =>
[1] => bar
)
PREG_SET_ORDER
结果使$ matches0是第一组匹配数组,$ matches1是第二组匹配数组,等等。
<?php
preg_match_all("|<[^>]+>(.*)</[^>]+>|U",
????"<b>example:?</b><div?align=\"left\">this?is?a?test</div>",
????$out,?PREG_SET_ORDER);
echo?$out[0][0]?.?",?"?.?$out[0][1]?.?"\n";
echo?$out[1][0]?.?",?"?.?$out[1][1]?.?"\n";
?>
上面的例子将输出:
<b>example: </b>, example:
<div align="left">this is a test</div>, this is a test
PREG_OFFSET_CAPTURE
如果此标志被传递,则对于每次发生的匹配,附属字符串偏移量也将被返回。 请注意,这会将匹配值更改为数组数组,其中每个元素都是由偏移量为0的匹配字符串组成的数组,并且它的字符串偏移量为偏移量为1的主体。
<?php
preg_match_all('/(foo)(bar)(baz)/',?'foobarbaz',?$matches,?PREG_OFFSET_CAPTURE);
print_r($matches);
?>
上面的例子将输出:
Array
(
[0] => Array
(
[0] => Array
(
[0] => foobarbaz
[1] => 0
)
)
[1] => Array
(
[0] => Array
(
[0] => foo
[1] => 0
)
)
[2] => Array
(
[0] => Array
(
[0] => bar
[1] => 3
)
)
[3] => Array
(
[0] => Array
(
[0] => baz
[1] => 6
)
)
)
如果没有给出命令标记,PREG_PATTERN_ORDER
则被假定。
offset
通常,搜索从主题字符串的开始处开始。 可选参数偏移量可用于指定开始搜索的替代位置(以字节为单位)。
注意:
?? 使用偏移不等同于将substr($ subject,$ offset)传递给preg_match_all()来代替主题字符串,因为模式可以包含断言,如^,$或(?<= x)。 有关示例,请参阅preg_match()。
返回值
返回完整模式匹配的数量(可能为零),如果发生错误,则返回FALSE。
更新日志
版 | 描述 |
---|---|
5.4.0 | matches参数变为可选。 |
5.3.6 | 如果偏移高于主题长度,则返回FALSE。 |
5.2.2 | 已命名的子模式现在接受语法(?<名称>)和(?'名称')以及(?P <名称>)。以前的版本只接受(?P <名称>)。 |
例子
示例#1 从一些文本中获取所有电话号码。
<?php
preg_match_all("/\(???(\d{3})???\)???(?(1)??[\-\s]?)?\d{3}-\d{4}/x",
????????????????"Call?555-1212?or?1-800-555-1212",?$phones);
?>
Example#2 查找匹配的HTML标签(greedy)
<?php
//?The?\\2?is?an?example?of?backreferencing.?This?tells?pcre?that
//?it?must?match?the?second?set?of?parentheses?in?the?regular?expression
//?itself,?which?would?be?the?([\w]+)?in?this?case.?The?extra?backslash?is
//?required?because?the?string?is?in?double?quotes.
$html?=?"<b>bold?text</b><a?href=howdy.html>click?me</a>";
preg_match_all("/(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/",?$html,?$matches,?PREG_SET_ORDER);
foreach?($matches?as?$val)?{
????echo?"matched:?"?.?$val[0]?.?"\n";
????echo?"part?1:?"?.?$val[1]?.?"\n";
????echo?"part?2:?"?.?$val[2]?.?"\n";
????echo?"part?3:?"?.?$val[3]?.?"\n";
????echo?"part?4:?"?.?$val[4]?.?"\n\n";
}
?>
上面的例子将输出:
matched: <b>bold text</b>
part 1: <b>
part 2: b
part 3: bold text
part 4: </b>
matched: <a href=howdy.html>click me</a>
part 1: <a href=howdy.html>
part 2: a
part 3: click me
part 4: </a>
示例#3 使用命名的子模式
<?php
$str?=?<<<FOO
a:?1
b:?2
c:?3
FOO;
preg_match_all('/(?P<name>\w+):?(?P<digit>\d+)/',?$str,?$matches);
/*?This?also?works?in?PHP?5.2.2?(PCRE?7.0)?and?later,?however?
?*?the?above?form?is?recommended?for?backwards?compatibility?*/
//?preg_match_all('/(?<name>\w+):?(?<digit>\d+)/',?$str,?$matches);
print_r($matches);
?>
上面的例子将输出:
Array
(
[0] => Array
(
[0] => a: 1
[1] => b: 2
[2] => c: 3
)
[name] => Array
(
[0] => a
[1] => b
[2] => c
)
[1] => Array
(
[0] => a
[1] => b
[2] => c
)
[digit] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[2] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
扩展内容
- preg_quote() - 引用正则表达式字符
- preg_match() - 执行正则表达式匹配
- preg_replace() - 执行正则表达式搜索并替换
- preg_split() - 用正则表达式分割字符串
- preg_last_error() - 返回上一次PCRE正则表达式执行的错误代码
← preg_last_error
preg_match →
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com