无法加载和解析DOM对象 [英] Can't Load And Parse DOM Object
本文介绍了无法加载和解析DOM对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试加载一个dom对象并解析以查找特定链接
这是我的代码:
I am trying to load a dom object and to parse it for finding specific links
Here is my code:
$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){}
object(DOMDocument)#1 (0) { }
如果我回显$内容, HTML。
If i echo the $content I get all the html.
我的代码有什么问题?
推荐答案
手册中的示例: 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);
?>
这篇关于无法加载和解析DOM对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文