Re: PHP Class for IP CIDR notation

From: Date: Thu, 05 Jun 2003 15:31:46 +0000
Subject: Re: PHP Class for IP CIDR notation
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-150371@lists.php.net to get a copy of this message
Here's a quick function of the top of my head. I'm not sure if x.x.0.x or x.0.x.x are valid IP addresses. Adjust as necessary. <? function cidr($initial_ip, $net_prefix=32) { $octet=explode(".",$initial_ip); $num_ips=pow(2,(32-$net_prefix)); $ip_list[]=implode(".",$octet); for ($i=1;$i<$num_ips;$i++) { if ($octet[3]<256) { $octet[3]++; } if ($octet[3]==256) { $octet[2]++; $octet[3]=0; } if ($octet[2]==256) { $octet[1]++; $octet[2]=0; } if ($octet[1]==256) { $octet[0]++; $octet[1]=0; } if ($octet[0]>255) { echo "First octet of IP address cannot exceed 255"; return false; } $ip_list[]=implode(".",$octet); } return $ip_list; } $arr=cidr("192.168.1.0",23); echo "<pre>"; print_r($arr); echo "</pre>"; ?> --- Rob Lacey <php@extrememetal.co.uk> wrote: > does anyone know of a class that is able to extrapolate a list of > IPs given an > address in cidr notation. I have used the Net::Netmask module in > perl, so I > was just wondering if there was a PHP equivalent. > > Thanks > > Rob > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > ===== Mark Weinstock mark_weinstock@yahoo.com *************************************** You can't demand something as a "right" unless you are willing to fight to death to defend everyone else's right to the same thing. *************************************** __________________________________ Do you Yahoo!? Yahoo! Calendar - Free online calendar with sync to Outlook(TM). http://calendar.yahoo.com

« previous php.general (#150371) next »