PHP LIBXML_NOWARNING not suppressing warnings?

Using the parameter flag LIBXML_NOWARNING does not stop wanrings when loading html using PHPDOMDocument-> loadHTML. Other constants really work.

In the example below, I add LIBXML_HTML_NODEFDTD to prove that the constants are received (stops adding doctype).

$doc=new DOMDocument(); $doc->loadHTML("<tagthatdoesnotexist><h1>Hi</h1></tagthatdoesnotexist>",LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_HTML_NODEFDTD); echo $doc->saveHTML(); 

However, warnings are still generated and displayed. What am I missing?

+5
php libxml2
Jan 25 '17 at 6:22
source share
1 answer

That the LIBXML_NOWARNING parameter LIBXML_NOWARNING ignored using DOMDocument::loadHTML() is a bug in PHP (and a fix). Recently, it was raised in the related issue "libxml htmlParseDocument, ignoring htmlParseOption flags" and filed as PHP Error # 74004 LIBXML_NOWARNING flag ingnored on loadHTML * .

However, you can manage error handling yourself until the error is resolved:

  • Set libxml_use_internal_errors(true) before calling DOMDocument::loadHTML . This will prevent errors from occurring before your default error handler. And you can get them (if you want) using other libxml error functions (e.g. libxml_get_errors() ).
  • When using this function, be sure to clear the internal error buffer. If you do not, and you use it in a lengthy process, you may find that all your memory is exhausted.
  • If you want to restore the default libxml_use_internal_errors() set libxml_use_internal_errors() .

Code example:

 $doc = new DOMDocument(); # clear errors list if any libxml_clear_errors(); # use internal errors, don't spill out warnings $previous = libxml_use_internal_errors(true); $doc->loadHTML("<tagthatdoesnotexist><h1>Hi</h1></tagthatdoesnotexist>"); echo $doc->saveHTML(); # clear errors list if any libxml_clear_errors(); # restore previous behavior libxml_use_internal_errors($previous); 



Update

Now this error is fixed.

+7
Jan 25 '17 at 6:42 on
source share



All Articles