我尝试将以下代码用于 HTML 页面,但它不起作用。如何在一页中检索和操作所有输出的 HTML 元素?
$doc = new DOMDocument;
$doc->load('http://localhost/foo/index.php');
$items = $doc->getElementsByTagName('img');
foreach ($items as $item) {
echo $item->nodeValue . "\n";
}
编辑:
$dom = new DOMDocument;
$html = 'http://localhost/foo/index.php';
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('a') as $node) {
echo $dom->saveHtml($node), PHP_EOL;
}
上面的代码什么也不输出
调试代码:
<?php
$dom = new DOMDocument;
$html = 'http://localhost/foo/index.php';
var_dump($dom->loadHTML($html));
echo '<br />';
var_dump($dom);
echo '<br />';
var_dump($dom->saveHTML());
echo '<br />';
var_dump($dom->getElementsByTagName('a'));
echo '<br />';
foreach ($dom->getElementsByTagName('a') as $node) {
var_dump($node);
echo '<br />';
var_dump( $dom->saveHtml($node) );
echo '<br />';
}
?>
调试结果:
bool(true)
object(DOMDocument)#1 (0) { }
string(170) "
http://localhost/foo/index.php
"
object(DOMNodeList)#2 (0) { }