简体   繁体   English

PHP正则表达式将子匹配作为数组返回

[英]PHP Regular expression return submatches as array

I have a question regarding regular expressions. 我有关于正则表达式的问题。

What I want to do is use only one regular expression to match part of string and get out what's inside divided. 我想要做的是只使用一个正则表达式来匹配字符串的一部分,并获得内部划分的内容。 Don't know how to explain it, so will write an example 不知道如何解释,所以会写一个例子

Example html to parse 要解析的示例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>

I want to preg_match(_all) only span values from .test 我想preg_match(_all)只跨越.test的值

Normally, I would use 通常,我会用

preg_match('/<div class="test">(.*?)<\/div>/', $html, $matches)
preg_match_all('/<span>(.*?)<\/span>/',  $matches[1],  $matches2)

And then use another preg_match_all to get out values. 然后使用另一个preg_match_all来获取值。

However, i was wondering if there is a way to make a subpattern in a pattern that would automaticly first match divs and then all spans and would return resulat as array. 但是,我想知道是否有一种方法可以在一个模式中创建一个子模式,该模式会自动首先匹配div,然后是所有跨度,并将resulat作为数组返回。

Is something like this possible? 这样的事情可能吗? I couldn't find it anywhere. 我无处可寻。 Maybe I don't know how it is technically called. 也许我不知道它在技术上是怎么称呼的。

Edit: Output I would like to get (changed data sample), but only with one preg_match or preg_match_all call : 编辑:输出我想得到(更改数据样本),但只有一个preg_match或preg_match_all调用

array(
    'a',
    'b',
    'c',
    'd',
);

Use a DOMParser instead of going for regular expressions.. 使用DOMParser而不是正则表达式..

$dom = new DOMDocument;
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('div') as $tag) {
    if ($tag->getAttribute('class') === 'test')
    {
        foreach($tag->getElementsByTagName('span') as $stag)
        {
        $val[]=$stag->nodeValue;
        }
    }
}
print_r($val);

Using an XPath Query.. (for the same) 使用XPath查询..(相同)

$xpath = new DOMXpath($dom);
$elements = $xpath->query("*/div[@class='test']/span");
foreach($elements as $v)
{
    $arr[]=$v->nodeValue;
}
print_r($arr);

OUTPUT :

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)

Working Demo - Normal DOM Way 工作演示 - 正常的DOM方式

Working Demo - XPath Way 工作演示 - XPath方式

Is this what you want: 这是你想要的吗:

/<span>([^<]*)<\\/span>/ with preg_match_all /<span>([^<]*)<\\/span>/preg_match_all

Demo: http://regex101.com/r/yD6gM0 演示: http//regex101.com/r/yD6gM0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM