What are some potential issues with using preg_match_all to extract links from HTML code?

One potential issue with using preg_match_all to extract links from HTML code is that it may not be able to handle all possible variations of HTML link tags, such as different attributes or nested elements. To solve this issue, it is recommended to use a more robust HTML parsing library like DOMDocument or SimpleXMLElement, which can handle complex HTML structures more reliably.

$html = '<a href="https://example.com">Link 1</a><a href="https://example2.com">Link 2</a>';

$dom = new DOMDocument();
$dom->loadHTML($html);

$links = $dom->getElementsByTagName('a');
foreach ($links as $link) {
    echo $link->getAttribute('href') . "\n";
}