note 35699 deleted from function.preg-match by didou

From: Date: Sun, 23 May 2004 14:12:47 +0000
Subject: note 35699 deleted from function.preg-match by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-69992@lists.php.net to get a copy of this message
Note Submitter: mkr@binarywerks.dk ---- 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"; } } ?>

« previous php.notes (#69992) next »