How can PHP users best handle invalid HTML or XML documents when using DOMDocument and DOMXpath?

When using DOMDocument and DOMXPath in PHP, users can handle invalid HTML or XML documents by using the libxml_use_internal_errors function to suppress errors and then manually load the document content. This allows users to parse and manipulate the document without encountering errors due to invalid markup.

<?php

// Suppress errors caused by invalid HTML or XML
libxml_use_internal_errors(true);

// Load the document content manually
$doc = new DOMDocument();
$doc->loadHTMLFile('invalid_document.html');

// Use DOMXPath to query the document
$xpath = new DOMXPath($doc);
$results = $xpath->query('//div');

// Output the results
foreach ($results as $result) {
    echo $result->nodeValue . PHP_EOL;
}

// Clear any libxml errors
libxml_clear_errors();

?>