如何在preg_match_all php中获取组的值?

Ush*_*ich 0 php preg-match-all preg-match

嗨,我的模式是:

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

而字符串:

<div class="nwstxtpic">
                        <span id="bodyHolder_newstextDetail_nwstxtPicPane"><a href="xxxxx" target="_blank"><img alt="xxxxx" title="xxxxx" src='xxxxx' />
Run Code Online (Sandbox Code Playgroud)

好吧,我的用于查找和获取我在patern中定义的4个组的值的PHP代码是:

$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;
}
Run Code Online (Sandbox Code Playgroud)

我不知道如何获得这4组的价值

href="(.*)"

alt="(.*)"

title="(.*)"

src=\'(.*)\'
Run Code Online (Sandbox Code Playgroud)

你能帮我吗?谢谢.

cle*_*ong 6

preg_match_all()默认以模式顺序返回结果,这不是很方便.传递PREG_SET_ORDER标志,以便以更合理的方式排列数据:

$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;
}
Run Code Online (Sandbox Code Playgroud)