ufk ufk - 1 year ago 59
PHP Question

PHP SimpleXML: How can I load an HTML file?

When I try to load an HTML file as XML using

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.

Answer Source

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>';