The IPTOCIDR function, how to resolve

Source: Internet
Author: User
Iptocidr function
function Ip2cidr ($ip _start, $ip _end) {
if (Long2ip (Ip2long ($ip _start))! = $ip _start or Long2ip (Ip2long ($ip _end))! = $ip _end) return!trigger_error (' IP illegal ', 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 ("Invalid number of intervals $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";
}


Above is the function that the moderator greatly writes
IP2CIDR ("36.96.0.0", "36.223.255.255")
Execution result 36.96.0.0/9

Counter-check
http://www.itmop.com/tool/ipaddress.php
36.96.0.0/9-36.0.0.1-36.127.255.254 inconsistent with 36.96.0.0-36.223.255.255

There is a Web site, but you cannot see the conversion code
http://ip2cidr.com/
36.96.0.0-36.223.255.255
Convert to 36.96.0.0/11
36.128.0.0/10
36.192.0.0/11

Split interval is not good to master ...
------Solution--------------------
It's not a problem.
------Solution--------------------
 $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 illegal ' , 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
[Ten] = 36.96.4.0/22
[One] = 36.96.8.0/21
[[] = 36.96.16.0/20
[+] = 36.96.32.0/19
[+] = 36.96.64.0/18
[[] = 36.96.128.0/17
[+] = 36.97.0.0/16
[+] = 36.98.0.0/15
[+] = 36.100.0.0/14
[+] = 36.104.0.0/13
[+] = 36.112.0.0/12
[+] = 36.128.0.0/9
)


------Solution--------------------
$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
)


The link you gave, the last section was wrong.
------Solution--------------------
$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)

$IPL _start = Ip2long ($ip _start);
if ($IPL _start < 0) $ipl _start + = 0x100000000;

$IPL _end = Ip2long ($ip _end);
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.