When working with URLs in PHP, what are the advantages of using a HTML parser like DOMDocument over regex?
When working with URLs in PHP, using a HTML parser like DOMDocument is advantageous over regex because HTML is a complex language with nested structures that can be difficult to parse accurately using regular expressions. DOMDocument provides a more robust and reliable way to extract specific elements from HTML documents, making it easier to work with URLs contained within the HTML.
// Example code using DOMDocument to extract URLs from HTML
$html = '<a href="https://www.example.com">Example Website</a>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$links = $dom->getElementsByTagName('a');
foreach ($links as $link) {
$url = $link->getAttribute('href');
echo $url . "\n";
}