Using PHP, how do I validate that a string is a valid IP?
Examples of valid strings:
Just in case there's anyone that doesn't want to use the ip2long function, here is a simple function (Idea taken from a class in osTicket):
function is_ip( $ip = null ) {
if( !$ip or strlen(trim($ip)) == 0){
return false;
}
$ip=trim($ip);
if(preg_match("/^[0-9]{1,3}(.[0-9]{1,3}){3}$/",$ip)) {
foreach(explode(".", $ip) as $block)
if($block<0 || $block>255 )
return false;
return true;
}
return false;
}