I have the following function

function IsValidIpAddress( $IP )
{
	$result = preg_match("/^([1-9]|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-4])$/",$IP,$matches);
	return $result;
}

it matches address well but also matches address like

192.009.012.254

Any ideas on how i can fix this regex

    There shouldn't be anything invalid about that address should there? Yeah it can be shortened to 192.9.12.254, but 192.009.012.254 should still be a valid address.

      function validate_ip($ip)
      {
      $result=preg_match('/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/',$ip,$quads);
      return ($quads[1]<=255 && $quads[2]<=255 && $quads[3]<=255 && $quads[4]<=255);
      }
      

      Of course a one-liner would be

      function validate_ip($ip)
      { return long2ip(ip2long($ip))==$ip;
      }
      

        Originally posted by Weedpacket
        Of course a one-liner would be

        function validate_ip($ip)
        { return long2ip(ip2long($ip))==$ip;
        }
        

        [/B]

        Wow, how did you ever think of something as clean and efficient as that. Amazing! 😃

          Write a Reply...