note 48277 added to function.preg-match

From: Date: Fri, 17 Dec 2004 16:44:04 +0000
Subject: note 48277 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-81918@lists.php.net to get a copy of this message
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; } } ?> ---- Manual Page -- http://www.php.net/manual/en/function.preg-match.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+48277 Delete -- http://master.php.net/manage/user-notes.php?action=delete+48277&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48277&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48277&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48277&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48277&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48277&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+48277&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#81918) next »