preg_match_all
执行全局正则表达式匹配
preg_match_all()
函数返回在字符串中找到的模式的匹配数,并用找到的匹配填充变量。
查找字符串中所有 "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); } ?>
亲自试一试
使用 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); } ?>
亲自试一试