Snippets → PHP
Test whether URL exists
Test if an url exists or not.
function url_exists($url) {
$hdrs = @get_headers($url);
return is_array($hdrs) ? preg_match('/^HTTP\/\d+\.\d+\s+2\d\d\s+.*$/',$hdrs[0]) : false;
}
Source: http://us2.php.net/manual/en/function.file-exists.php#84918...
Nice function.
My only real comment to this is that i, personally, hate ternary operation.
I’d probably write it like this;
function url_exists($url=false) {
if ( $url != false )
{
$hdrs = @get_headers($url);
if ( is_array($hdrs) )
{
return preg_match(‘/^HTTP\/\d+\.\d+\s+2\d\d\s+.*$/’,$hdrs[0]);
}
return false;
}
return false;
}