note 48277 deleted from function.preg-match by colder

From: Date: Mon, 18 Jun 2007 18:19:24 +0000
Subject: note 48277 deleted from function.preg-match by colder
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-127591@lists.php.net to get a copy of this message
Note Submitter: satch666 at dot nospam dot hotmail dot com ---- some fix for the function valid_ipv4() proposed by selt: if trying, for example this wrong IP: 257.255.34.6, it is got as valid IP, getting as result: 57.255.34.6 the first subpattern of numbers defined at pattern matches with '257', because '57' is a valid string for '1?\d\d' pattern; this happens because it is not added there some logic for the string limits ...; i have tried using '^1?\d\d$', and it works, as we are saying in plain english: if the string has 3 chars, then it is starting by '1' digit and followed by other 2, ending the string there; if it has 2 chars, then both are any digit; any other case out of this 2 doesnt match the pattern; in other words, it is defined the subrange of numbers from '10' to '199' so the function would get as this (after modifying pattern and erasing a var, called $range, not used at function): <? function valid_ipv4($ip_addr) { $num="([0-9]|^1?\d\d$|2[0-4]\d|25[0-5])"; if(preg_match("/$num\.$num\.$num\.$num/",$ip_addr,$matches)) { return $matches[0]; } else { return false; } } ?>

« previous php.notes (#127591) next »