preg_match_all
执行一个全局正则表达式匹配
&reftitle.description;
intfalsepreg_match_all
stringpattern
stringsubject
arraymatches&null;
intflags0
intoffset0
搜索subject中所有匹配pattern给定正则表达式
的匹配结果并且将它们以flag指定顺序输出到matches中.
在第一个匹配找到后, 子序列继续从最后一次匹配位置搜索.
&reftitle.parameters;
pattern
要搜索的模式,字符串形式。
subject
输入字符串。
matches
多维数组,作为输出参数输出所有匹配结果, 数组排序通过flags指定。
flags
可以结合下面标记使用(注意不能同时使用PREG_PATTERN_ORDER和
PREG_SET_ORDER):
PREG_PATTERN_ORDER
结果排序为$matches[0]保存完整模式的所有匹配, $matches[1]
保存第一个子组的所有匹配,以此类推。
]+>(.*)[^>]+>|U",
"example: this is a test
",
$out, PREG_PATTERN_ORDER);
echo $out[0][0] . ", " . $out[0][1] . "\n";
echo $out[1][0] . ", " . $out[1][1] . "\n";
?>
]]>
&example.outputs;
example: , this is a test
example: , this is a test
]]>
因此, $out[0] 是包含匹配完整模式的字符串的数组,
$out[1] 是包含闭合标签内的字符串的数组。
如果正则表达式包含了带名称的子组,$matches 额外包含了带名称子组的键。
如果正则表达式里,子组名称重名了,则仅最右侧的子组储存在 $matches[NAME] 中。
foo)|(?bar)/',
'foo bar',
$matches,
PREG_PATTERN_ORDER
);
print_r($matches['match']);
?>
]]>
&example.outputs;
[1] => bar
)
]]>
PREG_SET_ORDER
结果排序为$matches[0]包含第一次匹配得到的所有匹配(包含子组),
$matches[1]是包含第二次匹配到的所有匹配(包含子组)的数组,以此类推。
]+>(.*)[^>]+>|U",
"example: this is a test
",
$out, PREG_SET_ORDER);
echo $out[0][0] . ", " . $out[0][1] . "\n";
echo $out[1][0] . ", " . $out[1][1] . "\n";
?>
]]>
&example.outputs;
example: , example:
this is a test
, this is a test
]]>
PREG_OFFSET_CAPTURE
如果这个标记被传递,每个发现的匹配返回时会增加它相对目标字符串的字节偏移量。
注意这会改变matches中的每一个匹配结果字符串元素,使其
成为一个第0个元素为匹配结果字符串,第1个元素为
匹配结果字符串在subject中的偏移量。
]]>
&example.outputs;
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_UNMATCHED_AS_NULL
传入此标记,未匹配的子组报告为 &null;;否则会是空 string。
如果没有给定排序标记,假定设置为PREG_PATTERN_ORDER。
offset
通常, 查找时从目标字符串的开始位置开始。可选参数offset用于
从目标字符串中指定位置开始搜索(单位是字节)。
使用 offset 参数不同于传递 substr($subject, $offset)
的
结果到 preg_match_all 作为目标字符串,因为 pattern
可以包含断言比如^, $ 或者 (?<=x) 。
示例查看 preg_match。
&reftitle.returnvalues;
返回完整匹配次数(可能是 0),&return.falseforfailure;。
&reftitle.errors;
&pcre.pattern.warning;
&reftitle.changelog;
&Version;
&Description;
7.2.0
现在 $flags 参数可以支持 PREG_UNMATCHED_AS_NULL。
&reftitle.examples;
查找所有文本中的电话号码。
]]>
查找匹配的HTML标签(贪婪)
bold textclick me";
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";
}
?>
]]>
&example.outputs;
bold text
part 1:
part 2: b
part 3: bold text
part 4:
matched: click me
part 1:
part 2: a
part 3: click me
part 4:
]]>
使用子命名组
\w+): (?P\d+)/', $str, $matches);
/* 选择方式 */
// preg_match_all('/(?\w+): (?\d+)/', $str, $matches);
print_r($matches);
?>
]]>
&example.outputs;
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
)
)
]]>
&reftitle.seealso;
PCRE 匹配
preg_quote
preg_match
preg_replace
preg_split
preg_last_error