2011-12-12 78 views
1

我试图加载DOM对象,并解析它为查找特定链接
这里是我的代码:无法加载和解析DOM对象

$content = file_get_contents("http://www.example.com/example.php"); 
$dom = new DOMDocument(); 
$dom->loadHTML($content); 
$dom->preserveWhiteSpace = false; 
var_dump($dom); 

我得到的是:

对象(DOM文档)#1(0){}

如果我呼应$内容我得到的所有的HTML。

我的代码有什么问题?

+1

你的代码没有问题..看起来非常好。你卡在哪里? –

+2

你的代码没有问题,但是你可能会错误地认为'var_dump'应该在这里产生什么。 :-) – Jon

回答

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); 
?> 
+0

我不知道什么是错的,但你的例子工作正常。 – lvil

+0

np,对手册链接有一个思考,给出一些很好的例子 –

1

相反的var_dump($ XML) 尝试: 回声$ XML-> saveHTML()方法;