0
從給定的標記我要提取的超級鏈接和超鏈接提取href屬性和使用價值PHP DOM解析器
<span></span>
<span>Chapter1</span>
<span><a href="page2.htm">Chapter2</a></span>
<span><a href="page3.htm">Chapter3</a></span>
的這種情況的所有頭銜,我已經寫了follwing代碼,但它不工作
$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
等