我想要檢索位於此span類屬性中的正文文本。使用DOM解析PHP(無結果)
<span id="" style="color:#525B64;">The quick brown fox jumped over the lazy dog.</span>
我在我的網絡服務器上測試過它,我沒有收到任何錯誤,但是網頁是空白的。我對此很陌生,所以我不知道該從哪裏出發。
這是我的代碼。
<?php
// Load remote file, supress parse errors
libxml_use_internal_errors(TRUE);
$dom = new DOMDocument;
$dom->loadHTMLFile('http://somewebpage.com');
libxml_clear_errors();
// use XPath to find all nodes with a class attribute of header
$xp = new DOMXpath($dom);
$nodes = $xp->query('//span[@class="msgBody"]');
// output first item's content
echo $nodes->item(0)->nodeValue;
?>
什麼是您試圖訪問的實際頁面?據我所知,此代碼有效。 – lonesomeday 2011-02-02 01:24:51