Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: DOMDocument loadHTML returns an error when using HTML5 tags

Such as <section>. Is there anything I can do about this?

like image 903
Hamster Avatar asked Feb 17 '11 19:02

Hamster


2 Answers

I've run into this issue with PHP's DOMDoc and XSL functions. You basically have to load the document as XML. Thats the only way I got the <video> tag to work.

Update: You can also try adding elements & entities to the <!DOCTYPE html5 > as long as $doc->resolveExternals = true.

like image 82
James Miller Avatar answered Oct 18 '22 00:10

James Miller


I don't know if you've tried the library ultimately pointed to in this answer:

DOM parser that allows HTML5-style </ in <script> tag

Html5lib solved the same issue you're experiencing for me (it was the <aside> and <nav> tags triggering my issues)

I was using this to parse html fragments, and the advantage is that it was a drop-in replacement for DOMDocument, as currently it uses DOMDocument as the output object, so no other functionality was broken in my implementation.

There is a note in the documentation that they are looking to move away from DOMDocument in the future.

html5lib for PHP can be found here: http://code.google.com/p/html5lib/downloads/detail?name=html5lib-php-0.1.tar.gz&can=2&q=

In the answer linked to above, there are further details on usage.

like image 1
Dave Espionage Avatar answered Oct 18 '22 01:10

Dave Espionage