How PHP implements IP2CIDR (generates multiple CIDR)

Source: Internet
Author: User
Do you have a great God? There are no functions that generate more than one CIDR, such as 1.120.0.0 1.159.255.255 generate 1.120.0.0/13 1.128.0.0/11


Reply to discussion (solution)

function Ip2cidr ($ip _start, $ip _end) {
if (Long2ip (Ip2long ($ip _start))! = $ip _start or Long2ip (Ip2long ($ip _end))! = $ip _end) return NULL;
$IPL _start= (int) ip2long ($ip _start);
$IPL _end= (int) ip2long ($ip _end);
if ($ipl _start>0 && $ipl _end<0) $delta = ($ipl _end+4294967296)-$IPL _start;
else $delta = $ipl _end-$ipl _start;
$netmask =str_pad (Decbin ($delta), +, "0", "str_pad_left");
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 NULL;
for ($mask =0; $mask <32; $mask + +) if ($netmask [$mask]==1) break;
if (Substr_count ($netmask, "0")! = $mask) return NULL;
Return "$ip _start/$mask";
This function cannot generate multiple CIDR

echo Ip2cidr (' 1.120.0.0 ', ' 1.127.255.255 '); 1.120.0.0/13function Ip2cidr ($ip _start, $ip _end) {  if (Long2ip (Ip2long ($ip _start))! = $ip _start or Long2ip ( Ip2long ($ip _end))! = $ip _end) return NULL;   $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 = Str_pad (Decbin ($delta), +, "0", str_pad_left);  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 NULL;  for ($mask =0; $mask <32; $mask + +) if ($netmask [$mask]==1) break;  if (Substr_count ($netmask, "0")! = $mask) return NULL;  

echo Ip2cidr (' 1.120.0.0 ', ' 1.127.255.255 '); 1.120.0.0/13function Ip2cidr ($ip _start, $ip _end) {  if (Long2ip (Ip2long ($ip _start))! = $ip _start or Long2ip ( Ip2long ($ip _end))! = $ip _end) return NULL;   $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 = Str_pad (Decbin ($delta), +, "0", str_pad_left);  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 NULL;  for ($mask =0; $mask <32; $mask + +) if ($netmask [$mask]==1) break;  if (Substr_count ($netmask, "0")! = $mask) return NULL;  


Moderator you wrote the same as my 2 floor. 1.120.0.0 1.159.255.255 This IP interval generates CIDR problems

Although splitting is not very difficult, how do you determine the split point?
Like what
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
is a method of dismantling
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
Another kind of demolition.

and
echo Ip2cidr (' 1.120.0.0 ', ' 1.159.255.255 ');
The reason for the failure is that the mask is
00000000001001111111111111111111
There's nothing wrong with it, it just can't be said in CIDR.

Notice that the
Echo Long2ip (Bindec (' 111111111111111111111 ') +ip2long (' 1.120.0.0 ')); 1.151.255.255
So the second method of demolition can be machine-implemented, and the first one seems to only be done by hand

Although splitting is not very difficult, how do you determine the split point?
Like what
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
is a method of dismantling
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
Another kind of demolition.

and
echo Ip2cidr (' 1.120.0.0 ', ' 1.159.255.255 ');
The reason for the failure is that the mask is
00000000001001111111111111111111
There's nothing wrong with it, it just can't be said in CIDR.

Notice that the
Echo Long2ip (Bindec (' 111111111111111111111 ') +ip2long (' 1.120.0.0 ')); 1.151.255.255
So the second method of demolition can be machine-implemented, and the first one seems to only be done by hand

Thank you, moderator, I'll take care of it.

IP2CIDR ("1.40.0.0", "1.44.255.255");
00000000000001001111111111111111
Echo Long2ip (Bindec (' 111111111111111111111 ') +ip2long (' 1.40.0.0 ')); 1.71.255.255 out of range of specified IP

0000000000000100 1111111111111111
To take this

Splitting is supposed to start from the small area.
It's a big chunk, it's definitely inappropriate.

Thank you for my groping down

Analyze the problem a bit more
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

End

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 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";}
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

Thank you, moderators, especially the recognition of their own lack of efforts to make good

  • 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.