假设输入为:
222.123.34.45和222.123.34.55
然后我需要输出它们之间的ip地址:
222.123.34.45 222.123.34.46 ... 222.123.34.55发布于 2009-12-21 12:26:59
发布于 2009-12-21 12:39:06
利用PHP的类型灵活性
利用IP地址实际上是数字这一事实(可能在
$ip2 = '222.123.34.55';$ips = array();
for($i=ip2long($ip);$i<=ip2long($ip2);$i++)
{
$ips[] = long2ip($i);
}
print_r($ips);发布于 2018-05-23 21:32:02
可以使用函数;$ipStr = "192.168.1.0/255";
function getIpsArrayFromStrRange($ipStr) {
$ipsArray = array();
//validate ip and check range existing
$regexp = '/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})(\/(\d{1,3}))?$/';
$matches = array();
if(preg_match($regexp,$ipStr,$matches)){
//if it is a range
if(isset($matches[6])){
$min_ip = min($matches[4],$matches[6]);
$max_ip = max($matches[4], $matches[6]);
for($i=$min_ip; $i<=$max_ip; $i++){
$ipsArray[] = "$matches[1].$matches[2].$matches[3].$i";
}
}else{
$ipsArray[] = $ipStr;
}
return $ipsArray;
}else{
return false;
}
}https://stackoverflow.com/questions/1938162
复制相似问题