Php Can';t加载和解析DOM对象
我正在尝试加载dom对象并对其进行解析以查找特定链接Php Can';t加载和解析DOM对象,php,dom,Php,Dom,我正在尝试加载dom对象并对其进行解析以查找特定链接 这是我的密码: $content = file_get_contents("http://www.example.com/example.php"); $dom = new DOMDocument(); $dom->loadHTML($content); $dom->preserveWhiteSpace = false; var_dump($dom); 我得到的是: 对象(DOMDocument)#1(0){} 如果我回显$
这是我的密码:
$content = file_get_contents("http://www.example.com/example.php");
$dom = new DOMDocument();
$dom->loadHTML($content);
$dom->preserveWhiteSpace = false;
var_dump($dom);
我得到的是:
对象(DOMDocument)#1(0){}
如果我回显$content,我会得到所有的html
我的代码有什么问题?这是手册中的一个示例:
loadHTML($content);
//空数组以保存要返回的所有链接
$links=array();
//循环遍历dom中的每个标记并将其添加到链接数组中
foreach($xml->getElementsByTagName('a')作为$link){
$links[]=array('url'=>$link->getAttribute('href'),'text'=>$link->nodeValue);
}
打印(链接);
?>
而不是var\u dump($xml)
尝试:
echo$xml->saveHTML() 你的代码没有问题。。看起来很好。你被困在哪里了?你的代码没有问题,但是你对
var\u dump
应该在这里产生什么的假设可能有问题。:-)np,思考一下手册链接,给出了一些很好的例子
<?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);
?>