What is the purpose of using preg_match_all and file_get_contents in PHP?
The purpose of using preg_match_all in PHP is to perform a global regular expression match on a string and return all matches. On the other hand, file_get_contents is used to read the contents of a file into a string. These functions are commonly used together when you want to extract specific data from a file or webpage using regular expressions.
// Example code snippet using preg_match_all and file_get_contents
$url = 'https://www.example.com';
$html = file_get_contents($url);
// Define the regular expression pattern to match
$pattern = '/<a href="([^"]+)">([^<]+)<\/a>/';
// Perform a global regular expression match on the HTML content
preg_match_all($pattern, $html, $matches);
// Output the matched links
foreach ($matches[1] as $index => $link) {
echo "Link: $link\n";
}