note 35699 added to function.preg-match

From: Date: Thu, 11 Sep 2003 12:52:39 +0000
Subject: note 35699 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-56136@lists.php.net to get a copy of this message
A (AFAIK) correct implementation of Ipv4 validation, this one supports optional ranges (CIDR notation) and it validates numbers from 0-255 only in the address part, and 1-32 only after the / <? function valid_ipv4($ip_addr) { $num="([0-9]|1?\d\d|2[0-4]\d|25[0-5])"; $range="([1-9]|1\d|2\d|3[0-2])"; if(preg_match("/^$num\.$num\.$num\.$num(\/$range)?$/",$ip_addr)) { return 1; } return 0; } $ip_array[] = "127.0.0.1"; $ip_array[] = "127.0.0.256"; $ip_array[] = "127.0.0.1/36"; $ip_array[] = "127.0.0.1/1"; foreach ($ip_array as $ip_addr) { if(valid_ipv4($ip_addr)) { echo "$ip_addr is valid<BR>\n"; } else { echo "$ip_addr is NOT valid<BR>\n"; } } ?> ---- Manual Page -- http://www.php.net/manual/en/function.preg-match.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35699 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35699&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35699&report=yes

« previous php.notes (#56136) next »