This is a replacement for gethostbynamel(), for *nix only, which uses the "host" program to lookup hostnames. Most people won't need this, but I had a temporary server problem where all the PHP DNS lookup functions were failing, so this works as a direct replacement:
function SafeGetHostByNameL($hostname) {
if (false) { // put conditional check here to use normal gethostbynamel()
return gethostbynamel($hostname);
} elseif (ereg('[0-9]{1,3}(\.[0-9]{1,3}){3}', $hostname)) {
return array($hostname);
} else {
$response = trim(`host $hostname`);
if (preg_match_all('/([a-z0-9\.\-]* is an alias for ([a-z0-9\.\-]*)\.['." \r\n".']+)*[a-z0-9\.\-]* has address ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/', trim($response), $matches, PREG_PATTERN_ORDER)) {
return $matches[3];
} elseif (eregi('^Host '.$hostname.' not found', $response, $matches)) {
return false;
}
}
return false;
}