php正则表达式 全局查找,执行一个全局正则表达式匹配 - PHP 7 中文文档

(PHP 4, PHP 5, PHP 7)

preg_match_all – 执行一个全局正则表达式匹配

说明

preg_match_all

( string $pattern

, string $subject

[, array &$matches

[, int $flags = PREG_PATTERN_ORDER

[, int $offset = 0

]]] ) : int

搜索subject中所有匹配pattern给定正则表达式

的匹配结果并且将它们以flag指定顺序输出到matches中.

在第一个匹配找到后, 子序列继续从最后一次匹配位置搜索.

参数

pattern

要搜索的模式,字符串形式。

subject

输入字符串。

matches

多维数组,作为输出参数输出所有匹配结果, 数组排序通过flags指定。

flags

可以结合下面标记使用(注意不能同时使用PREG_PATTERN_ORDER和

PREG_SET_ORDER):

PREG_PATTERN_ORDER

结果排序为$matches[0]保存完整模式的所有匹配, $matches[1]

保存第一个子组的所有匹配,以此类推。

preg_match_all("|]+>(.*)[^>]+>|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: ,

this is a test

example: , this is a test

因此, $out[0]是包含匹配完整模式的字符串的数组,

$out[1]是包含闭合标签内的字符串的数组。

如果正则表达式包含了带名称的子组,$matches 额外包含了带名称子组的键。

如果正则表达式里,子组名称重名了,则仅最右侧的自组储存在 $matches[NAME] 中。

preg_match_all(

'/(?J)(?foo)|(?bar)/',

'foo bar',

$matches,

PREG_PATTERN_ORDER

);

print_r($matches['match']);

?>

以上例程会输出:

Array

(

[0] =>

[1] => bar

)

PREG_SET_ORDER

结果排序为$matches[0]包含第一次匹配得到的所有匹配(包含子组),

$matches[1]是包含第二次匹配到的所有匹配(包含子组)的数组,以此类推。

preg_match_all("|]+>(.*)[^>]+>|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: , example:

this is a test
, this is a test

PREG_OFFSET_CAPTURE

如果这个标记被传递,每个发现的匹配返回时会增加它相对目标字符串的偏移量。

注意这会改变matches中的每一个匹配结果字符串元素,使其

成为一个第0个元素为匹配结果字符串,第1个元素为

匹配结果字符串在subject中的偏移量。

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

通常, 查找时从目标字符串的开始位置开始。可选参数offset用于

从目标字符串中指定位置开始搜索(单位是字节)。

Note:

使用 offset 参数不同于传递 substr($subject, $offset) 的

结果到 preg_match_all() 作为目标字符串,因为 pattern

可以包含断言比如^, $ 或者 (?<=x)。

示例查看 [preg_match()](php7/function.preg-match)。

返回值

返回完整匹配次数(可能是0),或者如果发生错误返回FALSE。

更新日志

版本

说明

5.4.0

参数matches成为可选的。

5.3.6

如果 offset

大于

subject 的程度,将返回 FALSE。

5.2.2

子命名分组语法可以接受(?),(?'name')以及

(?P)了。 之前版本仅接受(?P)方式。

范例

Example #1 查找所有文本中的电话号码。

preg_match_all("/(? (d{3})? )? (?(1) [-s] ) d{3}-d{4}/x",

"Call 555-1212 or 1-800-555-1212", $phones);

?>

Example #2 查找匹配的HTML标签(贪婪)

//\2是一个后向引用的示例. 这会告诉pcre它必须匹配正则表达式中第二个圆括号(这里是([w]+))

//匹配到的结果. 这里使用两个反斜线是因为这里使用了双引号.

$html = "bold textclick me";

preg_match_all("/(]*>)(.*?)(\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] . "nn";

}

?>

以上例程会输出:

matched: 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:

Example #3 使用子命名组

$str = <<

a: 1

b: 2

c: 3

FOO;

preg_match_all('/(?Pw+): (?Pd+)/', $str, $matches);

/* 下面代码在php 5.2.2(pcre 7.0)或更高版本下工作, 不过, 为了向后兼容

* 推荐使用上面的方式. */

// preg_match_all('/(?w+): (?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

)

)

参见

[PCRE 匹配](php7/pcre.pattern)

[preg_quote()](php7/function.preg-quote) – 转义正则表达式字符

[preg_match()](php7/function.preg-match) – 执行匹配正则表达式

[preg_replace()](php7/function.preg-replace) – 执行一个正则表达式的搜索和替换

[preg_split()](php7/function.preg-split) – 通过一个正则表达式分隔字符串

[preg_last_error()](php7/function.preg-last-error) – 返回最后一个PCRE正则执行产生的错误代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值