How can error handling be improved when loading HTML documents in PHP using DOMDocument?
When loading HTML documents in PHP using DOMDocument, error handling can be improved by setting the libxml_use_internal_errors() function to true before loading the document. This will allow you to capture any errors that occur during the loading process and handle them accordingly.
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTMLFile('example.html');
$errors = libxml_get_errors();
foreach ($errors as $error) {
// Handle or log the error as needed
}
libxml_clear_errors();