function fetchWebPage($url) {
// Load the web page content
$content = file_get_contents($url);
// Check if the content is successfully loaded
if ($content === false) {
// If the web page doesn't exist, load a local file for error
$content = file_get_contents('local_error_file.html');
} else {
// Get the base URL
$base_url = parse_url($url, PHP_URL_SCHEME) . '://' . parse_url($url, PHP_URL_HOST);
// Replace relative URLs with absolute URLs using the base URL
$content = preg_replace('/(href|src)="(?!http)([^"]+)/i', '$1="' . $base_url . '/$2', $content);
}
return $content;
}
// Usage
$url = 'http://example.com';
$webPageContent = fetchWebPage($url);
// Output the web page content
echo $webPageContent;
In this example, the fetchWebPage function takes a URL as a parameter and uses file_get_contents to load the web page content. If the content is successfully loaded, it replaces any relative URLs (both in the href and src attributes) with absolute URLs using the base URL. If the content cannot be loaded, it falls back to loading a local error file.
Note: This code assumes that the local error file exists and is accessible. You can replace 'local_error_file.html' with the appropriate path and filename for your local error file.