Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.2.2

Request #17662 Is there really need for a regex or does a PHP function do the trick?
Submitted: 2010-08-06 01:14 UTC
From: hm2k Assigned: mj
Status: Wont fix Package: Net_CheckIP (version SVN)
PHP Version: Irrelevant OS:
Roadmaps: (Not assigned)    
Subscription  


 [2010-08-06 01:14 UTC] hm2k (James Wade)
Description: ------------ if (!preg_match("/^[0-9]+$/", $oct[$i])) { Try: if (!is_numeric($oct[$i])) { See: http://talks.php.net/show/php-best-practices/36 Alternatively, this whole script could be replaced by the following script: <?php function checkip ($ip) { return preg_match('/^\s*(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\s*$/m',$ip); } ?>

Comments

 [2010-08-07 18:51 UTC] doconnor (Daniel O'Connor)
-Type: Bug +Type: Feature/Change Request
 [2010-08-09 13:41 UTC] hm2k (James Wade)
Instead of if (!is_numeric($oct[$i])) { Try: if (!is_int($oct[$i])) { Obviously is_numeric includes decimals, which is no good here.
 [2011-02-15 22:46 UTC] hm2k (James Wade)
 [2011-02-15 22:46 UTC] hm2k (James Wade)
Patch attached. I see no reason not to include this improvement.
 [2011-02-16 19:03 UTC] hm2k (James Wade)
-Assigned To: +Assigned To: mj
 [2011-03-01 01:10 UTC] mj (Martin Jansen)
-Status: Assigned +Status: Wont fix
Using is_int() is not right here because it only works if the given variable is acutally of type int already: <?php var_dump(is_int("123")); var_dump(is_int(123)); ?> will actually print bool(false) bool(true) If we were to change something in the really really ancient code, we could completely get rid of the preg_match() check, because the next check will work just fine for rejecting any input that is not an integer between 0 and 255.
 [2011-03-01 02:19 UTC] hm2k (James Wade)
-Status: Wont fix +Status: Assigned
The idea here is to completely get rid of the preg_match, which isn't required. - if (!preg_match("/^[0-9]+$/", $oct[$i])) { + if (!is_numeric($oct[$i]) || ceil($oct[$i]) != $oct[$i]) {
 [2011-03-01 02:55 UTC] mj (Martin Jansen)
-Status: Assigned +Status: Wont fix
James, again, what you suggested will not work. <?php $var = "+127"; var_dump(!is_numeric($var) || ceil($var) != $var); ?> will result in bool(false) Thus with your patch the input "+127.0.0.1" will yield a positive return value, which clearly is bogus. Using ctype_digit() will probably work, but I'm hesitating to go this route because I don't intend to add an extension dependency. I suggest you stop investing so much of your time in this old package and focus on Net_CheckIP2 instead. Thanks.