Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need to parse some HTML files, however, they are not well-formed and PHP prints out warnings to. I want to avoid such debugging/warning behavior programatically. Please advise. Thank you!

Code:

// create a DOM document and load the HTML data $xmlDoc = new DomDocument; // this dumps out the warnings $xmlDoc->loadHTML($fetchResult); 

This:

@$xmlDoc->loadHTML($fetchResult) 

can suppress the warnings but how can I capture those warnings programatically?

like image 658
Viet Avatar asked Jul 19 '09 00:07

Viet


1 Answers

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 } 
like image 51
thomasrutter Avatar answered Sep 19 '22 07:09

thomasrutter