PHP的SimpleXML:我如何加载一个HTML文件? [英] PHP SimpleXML: How can I load an HTML file?

查看:101
本文介绍了PHP的SimpleXML:我如何加载一个HTML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用 simplexml_load_string 将HTML文件加载为XML时, 我收到很多关于HTML的错误和警告,并且它失败了,有没有办法使用SimpleXML正确加载html文件?



这个HTML文件可能有不需要的空间和其他一些我希望SimpleXML忽略的错误。 我会建议使用 PHP简单的HTML DOM 。我自己使用它来处理从抓取页面到操作HTML模板文件的任何内容,它非常简单,功能强大,应该适合您的需求。

以下是一些示例从他们的文档中可以看出您可以执行的操作:

  //从网址或文件创建DOM 
$ html = file_get_html('http://www.google.com/');

//查找所有图片
foreach($ html-> find('img')as $ element)
echo $ element-> src。 <峰; br>;

//查找所有链接
foreach($ html-> find('a')as $ element)
echo $ element-> href。 <峰; br>;


When I try to load an HTML file as XML using simplexml_load_string I get many errors and warnings regarding the HTML and it fails, it there a way to properly load an html file using SimpleXML?

This HTML file may have unneeded spaces and maybe some other errors that I would like SimpleXML to ignore.

解决方案

I would suggest using PHP Simple HTML DOM. I've used it myself for anything from page scraping to manipulating HTML template files and its very simple and quite powerful and should suit your needs just fine.

Here's a few examples from their docs that show the kind of things you can do:

// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');

// Find all images
foreach($html->find('img') as $element)
       echo $element->src . '<br>';

// Find all links
foreach($html->find('a') as $element)
       echo $element->href . '<br>'; 

这篇关于PHP的SimpleXML:我如何加载一个HTML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆