简体   繁体   中英

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

<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

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.

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.

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 :

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

Use a DOMParser instead of going for regular expressions..

$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 = 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

Working Demo - XPath Way

Is this what you want:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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