note 36532 modified in function.ip2long by danbrown

From: Date: Fri, 05 Dec 2008 13:46:50 +0000
Subject: note 36532 modified in function.ip2long by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-147708@lists.php.net to get a copy of this message
<?php function CalculateIPRange($iprange) { // Daevid Vincent [daevid@daevid.com] 10.13.03 // This function will return an array of either a negative error code // or all possible IP addresses in the given range. // format is NNN.NNN.NNN.NNN - NNN.NNN.NNN.NNN (spaces are okay) $temp = preg_split("/-/",$iprange, -1, PREG_SPLIT_NO_EMPTY); $QRange1 = $temp[0]; $QRange2 = $temp[1]; if ($QRange2 == "") return array($QRange1); //special case, they didn't put a second quad parameter //basic error handling to see if it is generally a valid IP in the form N.N.N.N if ( preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/",$QRange1) != 1 ) return array(-1); if ( preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/",$QRange2) != 1 ) return array(-1); $quad1 = explode(".",$QRange1); $quad2 = explode(".",$QRange2); reset ($quad1); while (list ($key, $val) = each ($quad1)) { $quad1[$key] = intval($val); if ($quad1[$key] < 0 || $quad1[$key] > 255) return array(-2); } reset ($quad2); while (list ($key, $val) = each ($quad2)) { $quad2[$key] = intval($val); if ($quad2[$key] < 0 || $quad2[$key] > 255) return array(-2); } $startIP_long = sprintf("%u",ip2long($QRange1)); $endIP_long = sprintf("%u",ip2long($QRange2)); $difference = $endIP_long - $startIP_long; //echo "startIP_long = ".$startIP_long." and endIP_long = ".$endIP_long." difference = ".$difference."<BR>"; $ip = array(); $k = 0; for ($i = $startIP_long; $i <= $endIP_long; $i++) { $temp = long2ip($i); //this is a total hack. there must be a better way. $thisQuad = explode(".",$temp); if ($thisQuad[3] > 0 && $thisQuad[3] < 255) $ip[$k++] = $temp; } return $ip; } //CalculateIPRange() ?> --was-- function CalculateIPRange($iprange) { // Daevid Vincent [daevid@daevid.com] 10.13.03 // This function will return an array of either a negative error code // or all possible IP addresses in the given range. // format is NNN.NNN.NNN.NNN - NNN.NNN.NNN.NNN (spaces are okay) $temp = preg_split("/-/",$iprange, -1, PREG_SPLIT_NO_EMPTY); $QRange1 = $temp[0]; $QRange2 = $temp[1]; if ($QRange2 == "") return array($QRange1); //special case, they didn't put a second quad parameter //basic error handling to see if it is generally a valid IP in the form N.N.N.N if ( preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/",$QRange1) != 1 ) return array(-1); if ( preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/",$QRange2) != 1 ) return array(-1); $quad1 = explode(".",$QRange1); $quad2 = explode(".",$QRange2); reset ($quad1); while (list ($key, $val) = each ($quad1)) { $quad1[$key] = intval($val); if ($quad1[$key] < 0 || $quad1[$key] > 255) return array(-2); } reset ($quad2); while (list ($key, $val) = each ($quad2)) { $quad2[$key] = intval($val); if ($quad2[$key] < 0 || $quad2[$key] > 255) return array(-2); } $startIP_long = sprintf("%u",ip2long($QRange1)); $endIP_long = sprintf("%u",ip2long($QRange2)); $difference = $endIP_long - $startIP_long; //echo "startIP_long = ".$startIP_long." and endIP_long = ".$endIP_long." difference = ".$difference."<BR>"; $ip = array(); $k = 0; for ($i = $startIP_long; $i <= $endIP_long; $i++) { $temp = long2ip($i); //this is a total hack. there must be a better way. $thisQuad = explode(".",$temp); if ($thisQuad[3] > 0 && $thisQuad[3] < 255) $ip[$k++] = $temp; } return $ip; } //CalculateIPRange() http://php.net/manual/en/function.ip2long.php

« previous php.notes (#147708) next »