The easiest solution would be to just suppress the error:
echo @file_get_contents("http://asdsfsfsfsfsdfad.com");
However, error suppression is generally considered bad practise because you never know what went wrong, so it is better to have a handler that selectively handles errors, for instance
set_error_handler(function($code, $message) {
return ($code === E_WARNING && strpos($message, 'php_network_getaddresses'));
});
echo file_get_contents("http://asdsfsfsfsfsdfad.com");
This would suppress any E_WARNINGS with a message containing 'php_network_getaddresses'. Any other Warnings will not be suppressed.
In addition, you dont want Regex to parse HTML, but use an HTML Parser, like one of those given in
So you could do it with DOM. Again, either using Error Suppression (bad)
$dom = new DOMDocument;
@$dom->loadHTMLFile("http://asdsfsfsfsfsdfad.com");
$titles = $dom->getElementsByTagName('title');
echo $titles->length ? $dom->nodeValue : 'No Title found';
Or selectively suppressing network errors:
set_error_handler(function($code, $message) {
return ($code === E_WARNING && strpos($message, 'php_network_getaddresses'));
});
$dom = new DOMDocument;
$dom->loadHTMLFile("http://asdsfsfsfsfsdfad.com");
$titles = $dom->getElementsByTagName('title');
echo $titles->length ? $titles->item(0)->nodeValue : 'No Title found';
However, this will then result in parsing errors because loadHTMLFile will not return any HTML, so to suppress the parsing errors as well, you'd have to do:
set_error_handler(function($code, $message) {
return ($code === E_WARNING && strpos($message, 'php_network_getaddresses'));
});
libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTMLFile("http://asdsfsfsfsfsdfad.com");
libxml_clear_errors();
$titles = $dom->getElementsByTagName('title');
echo $titles->length ? $titles->item(0)->nodeValue : 'No Title found';