Re: Regular Expresion Work on IP address
| From: | 1LT John W. Holmes | Date: | Wed, 04 Dec 2002 19:23:54 +0000 |
| Subject: | Re: Regular Expresion Work on IP address | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-127022@lists.php.net to get a copy of this message | ||
> Now I am continuing on with the same script and I need to check for valid
> IP address form.
> Currently I have:
>
> $ip="255.255.255.0";
>
> if (preg_match ("/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/", $ip))
{
> print "Good Ip";
> } else {
> print "Bad Ip";
> }
>
> This does work kinda. It checks to see if the 4 sets of numbers are well
> there and numbers.
> The problem I am having is that it does not check to see if the IP is in a
> proper range ie less than 255.255.255.255
> So you could enter 256.256.256.999 and it will say that it is a good IP.
I think it'd be hard to verify the range with a regex. ip2long and long2ip
do not validate. So, an option would be to write your own little function
that splits the $ip on the period, verifies there are 4 parts, then checks
that each part is between 1 and 255 inclusive.
---John Holmes...