How can regular expressions be effectively used in PHP to parse anchor links in a webpage?
To parse anchor links in a webpage using regular expressions in PHP, you can use the preg_match_all function to match all anchor tags in the HTML content. You can then extract the href attribute value from each matched anchor tag to get the URLs of the links.
<?php
$htmlContent = '<a href="https://example.com/page1">Link 1</a><a href="https://example.com/page2">Link 2</a>';
preg_match_all('/<a\s[^>]*href=\"([^\"]*)\"[^>]*>(.*?)<\/a>/', $htmlContent, $matches);
$links = $matches[1];
foreach ($links as $link) {
echo $link . "\n";
}
?>