How can one differentiate similar matches in preg_match_all() when extracting data from a URL source code in PHP?

When using preg_match_all() to extract data from a URL source code in PHP, one can differentiate similar matches by using capturing groups in the regular expression pattern. By enclosing the specific parts of the pattern that need to be differentiated in parentheses, each captured group will be stored separately in the resulting array. This allows for easy identification and retrieval of the specific data needed.

// Example code snippet
$url = "https://www.example.com/page";
$html = file_get_contents($url);

// Define the pattern with capturing groups
$pattern = '/<a href="(.*?)">(.*?)<\/a>/';

// Perform the preg_match_all() function
preg_match_all($pattern, $html, $matches, PREG_SET_ORDER);

// Iterate through the matches and access the captured groups
foreach ($matches as $match) {
    $link = $match[1]; // URL
    $text = $match[2]; // Anchor text
    echo "Link: $link, Text: $text\n";
}