0

从给定的标记中,我必须提取超链接和超链接的所有标题

<span></span>
<span>Chapter1</span>
<span><a href="page2.htm">Chapter2</a></span>
<span><a href="page3.htm">Chapter3</a></span>

为此,我编写了以下代码,但它不起作用

$doc = new DOMDocument();
$doc->loadHTML($page_links);
$tags = $doc->getElementsByTagName('span');
foreach ($tags as $tag) {
    echo '\n'.$tag->nodeValue;
    if($tag->hasChildNodes()) {
    echo $tag->childNodes->getAttribute('href');
     } else {
            echo 'default.htm';
         }
}

我期待这个输出:

Chapter1 default.htm
Chapter2 page2.htm
Chapter3 page3.htm

等等

4

1 回答 1

0

你能试试这个吗?

$doc = new DOMDocument();
$doc->loadHTML($page_links);
$tags = $doc->getElementsByTagName('span');
for($i=0;$i<$tags->length;$i++){
    echo $tags->item($i)->nodeValue; 
    if($tags->item($i)->hasChildNodes()) {
            if($tags->item($i)->firstChild->nodeName=='a'){
            echo " ".$tags->item($i)->firstChild->getAttribute('href').'<br/>';

            }else{
            echo " default.htm<br/>";
            }
    }
}
于 2013-11-01T18:25:29.433 回答