【发布时间】:2015-02-26 11:30:25
【问题描述】:
我正在使用preg_match 在具有特定 src 或 href 模式的 xml 中搜索 img 元素。示例:< img src= "level1/level2/2837"、< img href ='level1/level2/54322'。
preg_match('/< *\/? *' // Begining of xml element
. 'img' // "img" atribute
. '[^>]*' // Any character except >
. '(?:src|href) *= *' // src or href attribute with possible spaces between =
. '(?:"|\')level1\/level2\/(\d+)(?:"|\')/', // "level1/level2/2837"
$subject, $matches);
它有效,但只返回第一个匹配项。
例如,如果主题有以下内容:
$subject = "< img src= 'level1/level2/2837'/> <p>something</p> < img href ='level1/level2/54322'";
我得到的结果是:
$matches => Array
(
[0] => '< img src= "level1/level2/2837"'
[1] => '2837'
)
如何获取$matches 数组中的所有匹配项?
我已经使用simplexml_load_string 来实现此目的,但想了解正则表达式如何与preg_match 一起使用。
【问题讨论】:
标签: php regex preg-match