PHP 怎么实现ip2cidr(生成多个cidr)

WBOY
Release: 2016-06-13 12:04:16
Original
1475 people have browsed it

PHP 如何实现ip2cidr(生成多个cidr)
各位大神有没有生成多个cidr的函数  例如1.120.0.0 1.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;<br />echo ip2cidr('1.120.0.0', '1.169.255.255'), PHP_EOL;<br />echo ip2cidr('1.120.0.0', '1.179.255.255'), PHP_EOL;<br /><br />function ip2cidr($ip_start,$ip_end) {<br />  if(long2ip(ip2long($ip_start))!=$ip_start or long2ip(ip2long($ip_end))!=$ip_end) return !trigger_error('ip 不合法', E_USER_NOTICE); <br />  $ipl_start = ip2long($ip_start);<br />  $ipl_end = ip2long($ip_end);<br />  if($ipl_start>0 && $ipl_end<0) $delta = ($ipl_end + 4294967296) - $ipl_start;<br />  else $delta = $ipl_end - $ipl_start;<br />  $netmask = sprintf('%032b', $delta);<br />  if(ip2long($ip_start)==0 && substr_count($netmask,"1")==32) return "0.0.0.0/0";<br />  if($delta<0 or ($delta>0 && $delta%2==0)) return !trigger_error("区间数量不合法 $delta", E_USER_NOTICE);<br />  for($mask=0; $mask<32; $mask++) if($netmask[$mask]==1) break;<br />  if(substr_count($netmask,"0")!=$mask) {<br />    $w = strrpos($netmask, '0') + 1;<br />    $m = pow(2, 32-$w) - 1;<br />    $ip_start = long2ip(($ipl_start & ~$m)+$m+1);<br />    return long2ip($ipl_start & ~$m) . "/$w," . ip2cidr($ip_start,$ip_end);<br />  };<br />  return "$ip_start/$mask";<br />} <br />
Copy after login
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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!