How can XPath be utilized with DOMDocument to extract specific elements from HTML content in PHP?
To utilize XPath with DOMDocument in PHP to extract specific elements from HTML content, you can load the HTML content into a DOMDocument object and then use XPath queries to select the desired elements based on their attributes, tags, or text content.
$html = '<html><body><div class="content"><p>Hello, World!</p></div></body></html>';
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$elements = $xpath->query("//div[@class='content']/p");
foreach ($elements as $element) {
echo $element->nodeValue; // Output: Hello, World!
}