What are the advantages of using SimpleXML or DOM over regular expressions for parsing HTML in PHP?

Parsing HTML using regular expressions can be error-prone and difficult to maintain, especially when dealing with complex HTML structures. SimpleXML and DOM provide a more structured and reliable way to parse HTML in PHP, making it easier to navigate and extract data from the HTML document.

$html = '<div><p>Hello, world!</p></div>';
$dom = new DOMDocument();
$dom->loadHTML($html);

$paragraph = $dom->getElementsByTagName('p')->item(0)->nodeValue;
echo $paragraph; // Output: Hello, world!