开发者

PHP: DOMDocument loadHTML returns an error when using HTML5 tags

开发者 https://www.devze.com 2023-02-11 07:25 出处:网络
Such as <section>.Is there anything I can do ab开发者_如何学Pythonout this?I\'ve run into this issue with PHP\'s DOMDoc and XSL functions. You basically have to load the document as XML. Thats t

Such as <section>. Is there anything I can do ab开发者_如何学Pythonout this?


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.


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.


try to ignore the error:

In my cases, I have several "widgets" in several files which acts as templates. For instance: template_header.html template_footer.html

<?php
//Load header
$dom = new DOMDocument();
@$dom->loadHTMLFile('template_header.html');
$xpath = new DOMXpath($dom);
$nodelist = $xpath->query('div[class="template-widget"]');
foreach($nodelist as $node){
    echo $dom->saveHTML($node);
}   
?>

Note that $dom->loadhtmlfile has and '@' to ognore the error.

0

精彩评论

暂无评论...
验证码 换一张
取 消