我正在尝试加载dom对象并解析它以查找特定链接
这是我的代码:
$content = file_get_contents("http://www.example.com/example.php");
$dom = new DOMDocument();
$dom->loadHTML($content);
$dom->preserveWhiteSpace = false;
var_dump($dom);
我得到的是:
object(DOMDocument)#1(0){}
如果我回复$ content,我会得到所有的HTML。
我的代码有什么问题?
答案 0 :(得分:1)
这是手册中的一个示例:DOMDocument
<?php
$content = file_get_contents("http://www.example.com/example.php");
$xml = new DOMDocument();
$xml->loadHTML($content);
// Empty array to hold all links to return
$links = array();
//Loop through each <a> tag in the dom and add it to the link array
foreach($xml->getElementsByTagName('a') as $link) {
$links[] = array('url' => $link->getAttribute('href'), 'text' => $link->nodeValue);
}
print_r($links);
?>
答案 1 :(得分:1)
而不是var_dump($ xml) 尝试: echo $ xml-&gt; saveHTML();