How can SimpleXMLElement and XPath be combined in PHP to parse and extract data from an HTML document?

To parse and extract data from an HTML document using SimpleXMLElement and XPath in PHP, you can load the HTML document into a SimpleXMLElement object and then use XPath queries to navigate and extract the desired data from the document.

// Load the HTML document into a SimpleXMLElement object
$html = file_get_contents('example.html');
$xml = new SimpleXMLElement($html);

// Use XPath to query and extract data from the document
$titles = $xml->xpath('//h1'); // Extract all <h1> elements
foreach ($titles as $title) {
    echo $title . "<br>";
}

$links = $xml->xpath('//a/@href'); // Extract all href attributes from <a> elements
foreach ($links as $link) {
    echo $link . "<br>";
}