How can file_get_contents() and preg_replace() be used in PHP to extract specific data from a webpage?

To extract specific data from a webpage using PHP, you can use file_get_contents() to retrieve the contents of the webpage and then use preg_replace() to extract the desired data using regular expressions. This allows you to parse the HTML content and extract specific information based on patterns or tags.

$url = 'https://www.example.com';
$html = file_get_contents($url);

// Extract specific data using preg_replace
$pattern = '/<h1>(.*?)<\/h1>/'; // Example pattern to extract h1 tag content
preg_match($pattern, $html, $matches);

// Output the extracted data
echo $matches[1];