简体   繁体   中英

How to get the value of groups in preg_match_all php?

Hi, My pattern is:

'<span\s+id="bodyHolder_newstextDetail_nwstxtPicPane"><a\s+href="(.*)"\s+target="_blank"><img\s+alt="(.*)"\s+title="(.*)"\s+src=\'(.*)\'\s+/>'

And the string:

<div class="nwstxtpic">
                        <span id="bodyHolder_newstextDetail_nwstxtPicPane"><a href="xxxxx" target="_blank"><img alt="xxxxx" title="xxxxx" src='xxxxx' />

Well, my php code for finding and getting the value of 4 groups that i have defined in patern is:

$picinfo=preg_match_all('/<span\s+id="bodyHolder_newstextDetail_nwstxtPicPane"><a\s+href="(.*)"\s+target="_blank"><img\s+alt="(.*)"\s+title="(.*)"\s+src=\'(.*)\'\s+/>/',$newscontent,$matches);
foreach ($matches[0] as $match) {
    echo $match;
}

I dont know how to get the value of these 4 groups

href="(.*)"

alt="(.*)"

title="(.*)"

src=\'(.*)\'

Whould you please Help me? Thank you.

preg_match_all() by default returns the result in pattern order, which is not very convenient. Pass the PREG_SET_ORDER flag so that the data is arranged in a more logical way:

$newscontent='<span id="bodyHolder_newstextDetail_nwstxtPicPane"><a href="xxxxx" target="_blank"><img alt="xxxxx" title="xxxxx" src=\'xxxxxbb\' />'; 

$picinfo=preg_match_all('/<span\s+id="bodyHolder_newstextDetail_nwstxtPicPane"><a\s+href="(.*)"\s+target="_blank"><img\s+alt="(.*)"\s+title="(.*)"\s+src=\'(.*)\'\s+\/>/',$newscontent,$matches,PREG_SET_ORDER);
foreach ($matches as $match) {
    $href = $match[1];
    $alt = $match[2];
    $title = $match[3];
    $src = $match[4];
    echo $title;
}

Your RegEx is correct, as the manual says, by default PREG_PATTERN_ORDER is followed which orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on.

So as in your case, $matches 1 will contain the href, $matches 2 will contain the alt and so on. Like,

for($i = 0; $i <= count($matches[0]); $i++ )
     echo "href = {$matches[1][$i]}, alt = {$matches[2][$i]}";

$matches[0] will contain the full matched strings.

BTW, it is always advisable to use an XML parser, try DOMDocument. The obligatory .

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