PHP 8.5.0 Alpha 2 available for testing

Voting

: four plus zero?
(Example: nine)

The Note You're Voting On

marco[DOT]ceppi[@T]seacrow[DOT]org
15 years ago
Anonymous has a good point (though I don't agree with pushing to execution to shell unless I have to. However this is a faster example (explode then loop is a little too intensive for a simple check)

<?php
function gethost ($ip)
{
//Make sure the input is not going to do anything unexpected
//IPs must be in the form x.x.x.x with each x as a number

if( preg_match('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.]
(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/'
, $ip) )
{
$host = `host $ip`;
return ((
$host ? end ( explode (' ', $host)) : $ip));
}
else
{
return
false;
}
}
?>

Though to be honest I would use:

<?php
function gethost ($ip)
{
return (
preg_match('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.]
(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/'
, $ip) ) ? gethostbyaddr($ip) : false;
}
?>

<< Back to user notes page

To Top