Re: [PHP] Regular Expresion Work on IP address
Dev, Nice work! Thanks for implementing the book's algorithm/proving it in PHP-PCRE, and I do like your extension into an internal/external function. I've not implemented your code (bookmarked for later) but some initial comments: - don't forget some of the other 'funnies' in the IP addressing scheme, most notably 127.0.0.1 = localhost (which is also an 'internal' address) - I'd need to read/remind myself of others... - in the $parts[0]>=255 type of construct, remember that the > is superfluous - or the RegEx is overly specific (hence the discussion point earlier) - also the $parts[0] is a string, so "255" will be (marginally) faster - similarly you could make $ip_state="0"; into a boolean expression (and function return value) which might enable the function call to read more smoothly - regardless, $ip_state is currently sometimes treated as a string and once as an integer. - I don't know your IP allocations but in net_check() shouldn't all the if() logical ops be ANDs? Any thoughts of extending it to work with IPng addresses (or whatever they're calling it these days)? Great stuff, keep it coming! (and when you've finished perhaps you could submit it to one of the script libraries/improve on what they might already have?) =dn > Well after reading John'ss and DL Neil's replys i came up with 2 functions. > The first is to check to see if the entry is a valid IP address and the > Next is to determine if it is a Private Network IP or not!. > > You can see this in action at: > http://www.my-tangled-web.net/codebank/code/ip_check.php > and the sorce for that page is at: > http://www.my-tangled-web.net/codebank/getsource.php?dir=&file=ip_check.php > > Please if you test it and find a problem let me know! > > Thanks > > > function ipcheck($ip_chk){ > $parts=explode(".",$ip_chk); > if (preg_match > ("/^([01]?\d\d?|2[0-4]\d|25[0-4])\.([01]?\d\d?|2[0-4]\d|25[0-4])\.([01]?\d\d ?|2[0-4]\d|25[0-4])\.([01]?\d\d?|2[0-4]\d|25[0-4])$/", > $ip_chk)) > { > if($parts[0]==0 || $parts[0]>=255 ||$parts[1]>=255 || > $parts[2]>=255 || $parts[3]>=255){ > $ip_state="0"; > } > elseif($parts[0]<255 && $parts[1]<255 && $parts[2]<255 && > $parts[3]<255){ > $ip_state="1"; > } > } > else { > $ip_state=0; > } > $this="that"; > return $ip_state; > } > > function net_check($ip_2_chk){ > $parts=explode(".",$ip_2_chk); > if($parts[0]==10){ > $network="internal"; > } > elseif($parts[0]==172 && $parts[1]>=16 || $parts[1]<=31){ > $network="internal"; > } > elseif($parts[0]==192 && $parts[1]==168){ > $network="internal"; > } > else { > $network="external"; > } > return $network; > }; > > At 07:47 PM 12/4/2002 +, DL Neil wrote: > >John, > > > > > 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. > > > > > >My other post on the subject notwithstanding, I agree. > > > >When I get time/when I get a round tuit, I'd like to do a performance test > >on the (short) IP RegEx against what you have outlined: > > > >explode against "." > >run resultant array through 0-255 validation function > >- and still have the semantic issues of 0.0.0.0, etc... > > > >Meantime, will sit here whistling and quietly muttering into my beard (and > >dreaming of tuits), > >=dn > > > > > >-- > >PHP General Mailing List (http://www.php.net/) > >To unsubscribe, visit: http://www.php.net/unsub.php > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expresion Work on IP address
Well after reading John'ss and DL Neil's replys i came up with 2 functions. The first is to check to see if the entry is a valid IP address and the Next is to determine if it is a Private Network IP or not!. You can see this in action at: http://www.my-tangled-web.net/codebank/code/ip_check.php and the sorce for that page is at: http://www.my-tangled-web.net/codebank/getsource.php?dir=&file=ip_check.php Please if you test it and find a problem let me know! Thanks function ipcheck($ip_chk){ $parts=explode(".",$ip_chk); if (preg_match ("/^([01]?\d\d?|2[0-4]\d|25[0-4])\.([01]?\d\d?|2[0-4]\d|25[0-4])\.([01]?\d\d?|2[0-4]\d|25[0-4])\.([01]?\d\d?|2[0-4]\d|25[0-4])$/", $ip_chk)) { if($parts[0]==0 || $parts[0]>=255 ||$parts[1]>=255 || $parts[2]>=255 || $parts[3]>=255){ $ip_state="0"; } elseif($parts[0]<255 && $parts[1]<255 && $parts[2]<255 && $parts[3]<255){ $ip_state="1"; } } else { $ip_state=0; } $this="that"; return $ip_state; } function net_check($ip_2_chk){ $parts=explode(".",$ip_2_chk); if($parts[0]==10){ $network="internal"; } elseif($parts[0]==172 && $parts[1]>=16 || $parts[1]<=31){ $network="internal"; } elseif($parts[0]==192 && $parts[1]==168){ $network="internal"; } else { $network="external"; } return $network; }; At 07:47 PM 12/4/2002 +, DL Neil wrote: John, > 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. My other post on the subject notwithstanding, I agree. When I get time/when I get a round tuit, I'd like to do a performance test on the (short) IP RegEx against what you have outlined: explode against "." run resultant array through 0-255 validation function - and still have the semantic issues of 0.0.0.0, etc... Meantime, will sit here whistling and quietly muttering into my beard (and dreaming of tuits), =dn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expresion Work on IP address
John, > 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. My other post on the subject notwithstanding, I agree. When I get time/when I get a round tuit, I'd like to do a performance test on the (short) IP RegEx against what you have outlined: explode against "." run resultant array through 0-255 validation function - and still have the semantic issues of 0.0.0.0, etc... Meantime, will sit here whistling and quietly muttering into my beard (and dreaming of tuits), =dn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expresion Work on IP address
Hello Dev, > 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. > > Any clue how I would change it to check for that I'm reading my way through Friedl's, "Mastering Regular Expressions", O'Reilly, 2002. This is discussed in pp187-9, but I haven't had a chance to 'play' with it yet (!) and indeed he comments that it is one of those topics that you can attempt to refine and refine, but may be equally well off by sticking with what you have (surround the individual octet values with parentheses and then perform the semantic checking in PHP - as you discuss). What he comes up with observes the shorter range of digits for the first two digit positions of a (possible) three digit octet expression (in decimal), eg the first digit is either 0, 1, or 2 or not present at all. So if the first digit is a 1, then the next digits can be anything. If the first digit is a 0 or a space/non-existent then... However if the first digit is a 2, then the next digit may not exceed 5 [0-5]. If it is 5 then the last digit is similarly constrained, but if the second digit is less than 5, the third can be any digit. NB the 0.0.0.0 and 255.255.255.255 issues remain Thus the main component (handling each octet) is the basic code: ([01]?\d\d?|2[0-4]\d|25[0-5])\. which in English comes out as something like: a zero and a one may be present and followed by a digit and possibly another digit, or a two will be present, followed by a digit in the range 0 through 4, followed by another digit, or a two and a five will be present followed by a digit in the range 0 through 5, and then a dot/separator. This turns the whole shebang into: ^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2 [0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$ I'm sorry, but that's going to be diabolic to read after the email has had at it. The ^ and $, begin/end are necessary to ensure that there aren't over-running digits afore or aft of the IP addr. NB this is NOT tested (by me). I'm not sure if this code (the options in particular), as it is written, can be pasted straight into PHP's version of PCRE. YMMV! If you try it, please send feedback, =dn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expresion Work on IP address
> 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... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php