What are the advantages of using a DOM parser over regular expressions for parsing HTML content in PHP?

Regular expressions are not well-suited for parsing complex HTML content because HTML is a nested structure that can be difficult to accurately match with regular expressions. Using a DOM parser, such as PHP's DOMDocument class, allows for easier and more reliable parsing of HTML content by representing the HTML as a tree structure that can be navigated and manipulated.

// Create a new DOMDocument object
$dom = new DOMDocument();
// Load the HTML content into the DOMDocument
$dom->loadHTML($html_content);
// Use DOMXPath to query specific elements in the HTML
$xpath = new DOMXPath($dom);
$elements = $xpath->query('//div[@class="example"]');
// Loop through the elements and do something with them
foreach ($elements as $element) {
    // Do something with the element
}