从给定的标记中,我必须提取超链接和超链接的所有标题
<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
等等