PHP 如何?ip2cidr(產生多個cidr)
各位大神有沒有產生多個cidr的函數 例如1.120.0.01.159.255.255 產生 1.120.0.0/13 1.128.0.0/11
------解決方案--------------------
雖然拆分並不是很困難,但你如何確定拆分點呢?
比如
echo ip2cidr('1.120.0.0', '1.127.255.255'); //1.120.0.0/13
echo ip2cidr('1.128.0.0', '1.159.255.255'); //1.128.0.0/11
是一種拆法
echo ip2cidr('1.120.0.0', '1.151.255.255'); //1.120.0.0/11
echo ip2cidr('1.152.0.0', '1.159.255.255'); //1.152.0.0/13
又是一種拆法
而
echo ip2cidr('1.120.0.0', '1.159.255.255');
失敗的原因是掩碼為
00000000001001111111111111111111
其實本身並沒有錯,只是不能 cidr 表示而已
注意到
echo long2ip(bindec('111111111111111111111')+ip2long('1.120.0.0')); //1.151.255.255
所以那個第二種拆法是可以機器實現的,而第一種似只能手工實現
------解決方案--------------------
把問題多分析一下
00000001011101110000000000000000 1.119.0.0
00000001011110000000000000000000 1.120.0.0
00000001011110010000000000000000 1.121.0.0
00000001011110100000000000000000 1.122.0.0
00000001011110110000000000000000 1.123.0.0
00000001011111000000000000000000 1.124.0.0
00000001011111010000000000000000 1.125.0.0
00000001011111100000000000000000 1.126.0.0
00000001011111110000000000000000 1.127.0.0
00000001100000000000000000000000 1.128.0.0
00000001100000010000000000000000 1.129.0.0
00000001100000100000000000000000 1.130.0.0
00000001100000110000000000000000 1.131.0.0
00000001100001000000000000000000 1.132.0.0
00000001100001010000000000000000 1.133.0.0
00000001100001100000000000000000 1.134.0.0
00000001100001110000000000000000 1.135.0.0
00000001100010000000000000000000 1.136.0.0
00000001100010010000000000000000 1.137.0.0
00000001100010100000000000000000 1.138.0.0
00000001100010110000000000000000 1.139.0.0
00000001100011000000000000000000 1.140.0.0
00000001100011010000000000000000 1.141.0.0
00000001100011100000000000000000 1.142.0.0
00000001100011110000000000000000 1.143.0.0
00000001100100000000000000000000 1.144.0.0
00000001100100010000000000000000 1.145.0.0
00000001100100100000000000000000 1.146.0.0
00000001100100110000000000000000 1.147.0.0
00000001100101000000000000000000 1.148.0.0
00000001100101010000000000000000 1.149.0.0
00000001100101100000000000000000 1.150.0.0
00000001100101110000000000000000 1.151.0.0
00000001100110000000000000000000 1.152.0.0
00000001100110010000000000000000 1.153.0.0
00000001100110100000000000000000 1.154.0.0
00000001100110110000000000000000 1.155.0.0
00000001100111000000000000000000 1.156.0.0
00000001100111010000000000000000 1.157.0.0
00000001100111100000000000000000 1.158.0.0
00000001100111110000000000000000 1.159.0.0
00000001101000000000000000000000 1.160.0.0
------解決方案--------------------
結束
echo ip2cidr('1.120.0.0', '1.159.255.255'), PHP_EOL;
echo ip2cidr('1.120.0.0', '1.169.255.255'), PHP_EOL;
echo ip2cidr('1.120.0.0', '1.179.255.255'), PHP_EOL;
function ip2cidr($ip_start,$ip_end) {
if(long2ip(ip2long($ip_start))!=$ip_start or long2ip(ip2long($ip_end))!=$ip_end) return !trigger_error('ip 不合法', E_USER_NOTICE);
$ipl_start = ip2long($ip_start);
$ipl_end = ip2long($ip_end);
if($ipl_start>0 && $ipl_end<0) $delta = ($ipl_end + 4294967296) - $ipl_start;
else $delta = $ipl_end - $ipl_start;
$netmask = sprintf('%032b', $delta);
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 !trigger_error("區間數量不合法 $delta", E_USER_NOTICE);
for($mask=0; $mask<32; $mask++) if($netmask[$mask]==1) break;
if(substr_count($netmask,"0")!=$mask) {
$w = strrpos($netmask, '0') + 1;
$m = pow(2, 32-$w) - 1;
$ip_start = long2ip(($ipl_start & ~$m)+$m+1);
return long2ip($ipl_start & ~$m) . "/$w," . ip2cidr($ip_start,$ip_end);
};
return "$ip_start/$mask";
}
1.120.0.0/13,1.128.0.0/11
1.120.0.0/15,1.112.0.0/12,1.128.0.0/15,1.128.0.0/13,1.136.0.0/15,1.138.0.0/11
1.120.0.0/14,1.120.0.0/13,1.128.0.0/14,1.128.0.0/12,1.144.0.0/14,1.148.0.0/11