What are the best practices for extracting both the link and text from an anchor tag in PHP using regex?

When extracting both the link and text from an anchor tag in PHP using regex, it is important to use capturing groups to extract the desired information. The link can be extracted using the href attribute within the anchor tag, while the text can be extracted from the content between the opening and closing anchor tags. By using regex with capturing groups, we can easily extract both the link and text from an anchor tag in PHP.

$html = '<a href="https://www.example.com">Example Website</a>';
$pattern = '/<a\s*href="([^"]*)"\s*>(.*?)<\/a>/';

preg_match($pattern, $html, $matches);

$link = $matches[1];
$text = $matches[2];

echo "Link: " . $link . "\n";
echo "Text: " . $text;