IpToCidr函數,該如何解決

來源:互聯網
上載者:User
IpToCidr函數
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";
}


上面是版主大大給寫的函數
ip2cidr("36.96.0.0","36.223.255.255")
執行結果36.96.0.0/9

反查
http://www.itmop.com/tool/ipaddress.php
36.96.0.0/9 -> 36.0.0.1 -36.127.255.254 與36.96.0.0 - 36.223.255.255 不一致

有個網站,但是無法看到轉碼
http://ip2cidr.com/
36.96.0.0-36.223.255.255
轉化為36.96.0.0/11
36.128.0.0/10
36.192.0.0/11

拆分區間不好掌握。。。
------解決方案--------------------
測試過沒有問題啊。
------解決方案--------------------
$r = ip2cidr("36.96.0.1","36.223.255.255");
print_r($r);

function ip2cidr($ip_start,$ip_end) {
$res = array();
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);
if($ipl_start < 0) $ipl_start += 0x100000000;

$ipl_end = ip2long($ip_end);
if($ipl_end<0) $ipl_end += 0x100000000;

$ipl=$ipl_start;
do {
$k = strrpos(sprintf('%032b', $ipl), '1');
$cidr = $k + 1;
$dk = pow(2, 32-$k-1);
$mask = $dk - 1;
$res[] = sprintf("%s/%d", long2ip($ipl & ~$mask), $cidr);
$ipl += $dk;
}while($ipl < $ipl_end);
return $res;
}
Array
(
[0] => 36.96.0.1/32
[1] => 36.96.0.2/31
[2] => 36.96.0.4/30
[3] => 36.96.0.8/29
[4] => 36.96.0.16/28
[5] => 36.96.0.32/27
[6] => 36.96.0.64/26
[7] => 36.96.0.128/25
[8] => 36.96.1.0/24
[9] => 36.96.2.0/23
[10] => 36.96.4.0/22
[11] => 36.96.8.0/21
[12] => 36.96.16.0/20
[13] => 36.96.32.0/19
[14] => 36.96.64.0/18
[15] => 36.96.128.0/17
[16] => 36.97.0.0/16
[17] => 36.98.0.0/15
[18] => 36.100.0.0/14
[19] => 36.104.0.0/13
[20] => 36.112.0.0/12
[21] => 36.128.0.0/9
)


------解決方案--------------------
$r = ip2cidr("36.96.0.0","36.223.255.255");
print_r($r);

Array
(
[0] => 36.96.0.0/11
[1] => 36.128.0.0/9
)


你給的那個連結,最後一節的結果是錯的
------解決方案--------------------
$r = ip2cidr("36.96.0.0","36.223.255.255");
print_r($r);

function ip2cidr($ip_start,$ip_end) {
$res = array();
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);
if($ipl_start < 0) $ipl_start += 0x100000000;

$ipl_end = ip2long($ip_end);
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.