What alternative methods can be used to parse and extract links from HTML content in PHP?

When parsing and extracting links from HTML content in PHP, one alternative method is to use regular expressions to search for anchor tags and extract the href attribute value. This allows for more flexibility in handling different types of HTML structures compared to using DOMDocument or SimpleXMLElement. However, it's important to be cautious when using regular expressions for parsing HTML, as they may not cover all edge cases.

$htmlContent = '<a href="https://www.example.com">Example Link</a><a href="https://www.test.com">Test Link</a>';

preg_match_all('/<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU', $htmlContent, $matches);

$links = $matches[2];

foreach ($links as $link) {
    echo $link . "\n";
}