当前位置: 首页> 函数类别大全> preg_match_all

preg_match_all

执行全局正则表达式匹配
名称:preg_match_all
分类:正则处理PCRE
所属语言:php
一句话介绍:在字符串中查找模式的所有匹配项。

定义和用法

preg_match_all() 函数返回在字符串中找到的模式的匹配数,并用找到的匹配填充变量。

实例

例子 1

查找字符串中所有 "ain" 的出现:

<?php
$str = "The rain in SPAIN falls mainly on the plains.";
$pattern = "/ain/i";
if(preg_match_all($pattern, $str, $matches)) {
  print_r($matches);
}
?>

亲自试一试

例子 2

使用 PREG_PATTERN_ORDER 设置 matches 数组的结构。在此例中,matches 数组中的每个元素都有正则表达式分组之一的所有匹配项。

<?php
$str = "abc ABC";
$pattern = "/((a)b)(c)/i";
if(preg_match_all($pattern, $str, $matches, PREG_PATTERN_ORDER)) {
  print_r($matches);
}
?>

亲自试一试

同类函数