note 25958 added to ref.network

From: Date: Sat, 12 Oct 2002 20:49:51 +0000
Subject: note 25958 added to ref.network
Groups: php.notes 
Request: Send a blank email to php-notes+get-37985@lists.php.net to get a copy of this message
... and this one will do the opposite (o return NULL for invalid netblocks) : 1.0.0.0 1.0.255.255 -> 1.0.0.0/16 1.0.0.0 1.3.255.255 -> 1.0.0.0/14 192.168.0.0 192.168.0.255 -> 192.168.0.0/24 function ip2cidr($ip_start,$ip_end) { if(long2ip(ip2long($ip_start))!=$ip_start or long2ip(ip2long($ip_end))!=$ip_end) return NULL; $ipl_start=(int)ip2long($ip_start); $ipl_end=(int)ip2long($ip_end); if($ipl_start>0 && $ipl_end<0) $delta=($ipl_end+4294967296)-$ipl_start; else $delta=$ipl_end-$ipl_start; $netmask=str_pad(decbin($delta),32,"0","STR_PAD_LEFT"); if(ip2long($ip_start)==0 && substr_count($netmask,"1")==32) return "0.0.0.0/0"; if($delta<0 or ($delta>0 && $delta%2==0)) return NULL; for($mask=0;$mask<32;$mask++) if($netmask[$mask]==1) break; if(substr_count($netmask,"0")!=$mask) return NULL; return "$ip_start/$mask"; } -- http://www.php.net/manual/en/ref.network.php http://master.php.net/manage/user-notes.php?action=edit+25958 http://master.php.net/manage/user-notes.php?action=delete+25958 http://master.php.net/manage/user-notes.php?action=reject+25958

« previous php.notes (#37985) next »