In PHP, what are the best practices for reading and extracting data from a webpage using file functions like file_get_contents?

When reading and extracting data from a webpage using file functions like file_get_contents in PHP, it is important to properly handle errors, sanitize input, and use regular expressions or DOM parsing to extract the desired data. Additionally, it is recommended to cache the retrieved data to avoid making multiple requests to the same webpage.

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

if ($html === false) {
    die('Error: Unable to retrieve webpage content.');
}

// Extract data using regular expressions or DOM parsing
// Example: 
// preg_match('/<title>(.*?)<\/title>/', $html, $matches);
// $title = $matches[1];

// Cache the retrieved data if needed
// Example:
// file_put_contents('cached_data.txt', $html);