【发布时间】:2014-06-04 16:56:48
【问题描述】:
我有一个关于正则表达式的问题。
我想要做的是只使用一个正则表达式来匹配字符串的一部分并取出里面的内容。不知道怎么解释,就写个例子吧
要解析的示例 html
<div class="test">
<span>a</span>
<span>b</span>
<span>c</span>
<span>d</span>
</div>
<div class="test2">
<span>aa</span>
<span>bb</span>
<span>cc</span>
<span>dd</span>
</div>
我只想 preg_match(_all) 跨越 .test 中的值
通常情况下,我会使用
preg_match('/<div class="test">(.*?)<\/div>/', $html, $matches)
preg_match_all('/<span>(.*?)<\/span>/', $matches[1], $matches2)
然后使用另一个 preg_match_all 来获取值。
但是,我想知道是否有一种方法可以在一个模式中创建一个子模式,该模式会自动首先匹配 div,然后是所有跨度,并将结果作为数组返回。
这样的事情可能吗?我在任何地方都找不到它。也许我不知道它在技术上是怎么称呼的。
编辑: 我想获得的输出(更改的数据样本),但只有一次 preg_match 或 preg_match_all 调用:
array(
'a',
'b',
'c',
'd',
);
【问题讨论】:
标签: php arrays regex function dom