21

Using the LIBXML_HTML_NOIMPLIED flag with an html fragment generates incorrect tags:

$str = '<p>Lorem ipsum dolor sit amet.</p><p>Nunc vel vehicula ante.</p>';
$doc = new DOMDocument();
$doc->loadHTML($str, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
echo $doc->saveHTML();

Outputs:

<p>Lorem ipsum dolor sit amet.<p>Nunc vel vehicula ante.</p></p>

I have found hacks to work around this using regexes, but that defeats the purpose of using DOM. I have tested this with several versions of libxml and php, the latest with libxml 2.9.2, php 5.6.7 (Debian Jessy). Any suggestions appreciated.

1
  • Unfortunately, Libxml re-arranges the document. If you note, it also prints a warning: the issue is that there isn't a single root element. You can either wrap the content in a div as the answer suggests or remove the LIBXML_HTML_NOIMPLIED option and use any other solution from stackoverflow.com/questions/4879946/… Sep 22, 2015 at 7:31

2 Answers 2

27

The re-arrangement is done by the LIBXML_HTML_NOIMPLIED option you're using. Looks like it's not stable enough for your case.

Also you might want to not use it for portablility reasons, for example I've got one PHP 5.4.36 with Libxml 2.7.8 at hand that is not supporting LIBXML_HTML_NOIMPLIED (Libxml >= 2.7.7) but later LIBXML_HTML_NODEFDTD (Libxml >= 2.7.8) option.

I know this way of dealing with it. When you load the fragment, you wrap it into a <div> element:

$doc->loadHTML("<div>$str</div>");

This helps to guide DOMDocument on the structure you want.

You can then extract this container from the document itself:

$container = $doc->getElementsByTagName('div')->item(0);
$container = $container->parentNode->removeChild($container);

And then remove all children from the document:

while ($doc->firstChild) {
    $doc->removeChild($doc->firstChild);
}

Now the document is completely empty and you're now able to append children again. Luckily there is the <div> container element we removed earlier, so we can add from it:

while ($container->firstChild ) {
    $doc->appendChild($container->firstChild);
}

The fragment then can be retrieved with the known saveHTML method:

echo $doc->saveHTML();

Which gives in your scenario:

<p>Lorem ipsum dolor sit amet.</p><p>Nunc vel vehicula ante.</p>

This methodology is a little different from the existing material here on site (see the references I give below), so the example at once:

$str = '<p>Lorem ipsum dolor sit amet.</p><p>Nunc vel vehicula ante.</p>';

$doc = new DOMDocument();
$doc->loadHTML("<div>$str</div>");

$container = $doc->getElementsByTagName('div')->item(0);
$container = $container->parentNode->removeChild($container);
while ($doc->firstChild) {
    $doc->removeChild($doc->firstChild);
}

while ($container->firstChild ) {
    $doc->appendChild($container->firstChild);
}

echo $doc->saveHTML();

I also really recommend the reference question on How to saveHTML of DOMDocument without HTML wrapper? for a further read as well as the one about inner-html

References

2
  • Thank you for your response, and a solution without regexes. I have rarely used DOMdocument in the past, but after a week of testing with large html fragments, I am finding the ad nauseum advice to use it over regexes dubious.
    – J.L. Hill
    Apr 7, 2015 at 20:41
  • 1
    Hmm, depends on what you want to achieve. There are times you need (or want have) a HTML parser. In PHP that means either DOMDocument or the tidy class (which also offers nodes but has no xpath). When you ask me personally: You can't beat xpath with regexes when it comes to HTML. I also try to give answers that offer a path for alternation between the two, for example here: stackoverflow.com/a/29481904/367456
    – hakre
    Apr 7, 2015 at 20:48
11

The LIBXML_HTML_NOIMPLIED option is not buggy, it's just badly documented. To fix the problem, wrap your input string with <html>…</html>, process your HTML, and then strip that off the output. LibXML requires a root node, and is treating the first element it finds as the root node, deleting the (incorrectly located) closing tag it finds half-way through, and then outputting the closing tag of the first element it found at the end of the document. It's logical when you see it from (Lib)XML's perspective.

3
  • LIBXML_HTML_NOIMPLIED also messes up the HTML code by removing the tabs, indents and the line breaks Jan 10, 2018 at 11:20
  • @ZoltánSüle Szia. Does that even apply within <PRE> and <PLAINTEXT> elements? If so, that is a bug I'd say. My own HTML usage is minified so I would have missed this. Jan 10, 2018 at 12:06
  • Szia. I just tried it out. If I wrap the HTML code with <PRE> or <PLAINTEXT> then LIBXML_HTML_NOIMPLIED doesn't remove the tabs and the indents. Jan 10, 2018 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.