DOMDocument::loadHTML error

Header, Nav and Section are elements from HTML5. Because HTML5 developers felt it is too difficult to remember Public and System Identifiers, the DocType declaration is just: <!DOCTYPE html> In other words, there is no DTD to check, which will make DOM use the HTML4 Transitional DTD and that doesnt contain those elements, hence the … Read more

What’s the difference between PHP’s DOM and SimpleXML extensions?

In a nutshell: SimpleXml is for simple XML and/or simple UseCases limited API to work with nodes (e.g. cannot program to an interface that much) all nodes are of the same kind (element node is the same as attribute node) nodes are magically accessible, e.g. $root->foo->bar[‘attribute’] DOM is for any XML UseCase you might have … Read more

PHP XML how to output nice format

You can try to do this: … // get completed xml document $doc->preserveWhiteSpace = false; $doc->formatOutput = true; $xml_string = $doc->saveXML(); echo $xml_string; You can make set these parameter right after you’ve created the DOMDocument as well: $doc = new DomDocument(‘1.0′); $doc->preserveWhiteSpace = false; $doc->formatOutput = true; That’s probably more concise. Output in both cases … Read more

Disable warnings when loading non-well-formed HTML by DomDocument (PHP)

Call libxml_use_internal_errors(true); prior to processing with with $xmlDoc->loadHTML() This tells libxml2 not to send errors and warnings through to PHP. Then, to check for errors and handle them yourself, you can consult libxml_get_last_error() and/or libxml_get_errors() when you’re ready: libxml_use_internal_errors(true); $dom->loadHTML($html); $errors = libxml_get_errors(); foreach ($errors as $error) { // handle the errors as you wish … Read more

Detect Document Height Change

Update (Oct 2020): resizeObserver is a wonderful API (support table) // create an Observer instance const resizeObserver = new ResizeObserver(entries => console.log(‘Body height changed:’, entries[0].target.clientHeight) ) // start observing a DOM node resizeObserver.observe(document.body) // click anywhere to rnadomize height window.addEventListener(‘click’, () => document.body.style.height = Math.floor((Math.random() * 5000) + 1) + ‘px’ ) click anywhere to … Read more

How to saveHTML of DOMDocument without HTML wrapper?

All of these answers are now wrong, because as of PHP 5.4 and Libxml 2.6 loadHTML now has a $option parameter which instructs Libxml about how it should parse the content. Therefore, if we load the HTML with these options $html->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); when doing saveHTML() there will be no doctype, no <html>, and … Read more

PHP DOMDocument errors/warnings on html5-tags

No, there is no way of specifying a particular doctype to use, or to modify the requirements of the existing one. Your best workable solution is going to be to disable error reporting with libxml_use_internal_errors: $dom = new DOMDocument; libxml_use_internal_errors(true); $dom->loadHTML(‘…’); libxml_clear_errors();