note 61985 added to function.preg-match

From: Date: Thu, 16 Feb 2006 19:12:00 +0000
Subject: note 61985 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-103855@lists.php.net to get a copy of this message
Correct IP matching Pattern: Max's IP match pattern fail on this IP '009.111.111.1', Chortos-2's pattern fail on both '09.111.111.1' and '009.111.111.1' Most of other patterns written also fail if you use them in preg_match_all, they return incorrect IP ie. $num="([0-9]|[0-9]{2}|1\d\d|2[0-4]\d|25[0-5])"; $test="127.0.0.112 10.0.0.2"; preg_match_all("/$num\\.$num\\.$num\\.$num/",$test,$match); print_r($match); will print "127.0.0.1" not "127.0.0.112"; so its wrong. To make my pattern compatible with preg_match_all IP matching (parsing multiple IPs) I write my pattern reverse order also. This is my new IP octet pattern probably perfect:) $num="(25[0-5]|2[0-4]\d|[01]?\d\d|\d)"; /* 25[0-5] => 250-255 2[0-4]\d => 200-249 [01]?\d\d => 00-99,000-199 \d => 0-9 */ <? $num="(25[0-5]|2[0-4]\d|[01]?\d\d|\d)"; $ip_addr='009.111.111.100'; if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $ip_addr,$match)) echo "Wrong IP Address\\n"; echo $match[0]; ?> <? $num="(25[0-5]|2[0-4]\d|[01]?\d\d|\d)"; $test="127.0.0.112 10.0.0.2"; preg_match_all("/$num\\.$num\\.$num\\.$num/",$test,$match); print_r($match); ?> ---- Server IP: 64.246.30.37 Probable Submitter: 85.101.173.95 ---- Manual Page -- http://www.php.net/manual/en/function.preg-match.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+61985 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61985&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61985&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61985&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61985&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61985&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61985&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61985&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+61985&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#103855) next »