PHP 如何实现ip2cidr(生成多个cidr)
各位大神有没有生成多个cidr的函数 例如1.120.0.0 1.159.255.255 生成 1.120.0.0/13 1.128.0.0/11
回复讨论(解决方案)
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 else $delta=$ipl_end-$ipl_start;
$netmask=str_pad(decbin($delta),32,"0","STR_PAD_LEFT");
if(ip2long($ip_start)==0 && substr_count($netmask,"1")==32) return "0.0.0.0/0";
if($delta0 && $delta%2==0)) return NULL;
for($mask=0;$mask if(substr_count($netmask,"0")!=$mask) return NULL;
return "$ip_start/$mask";
} 这个函数不能生成多个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), 32, "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";}
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), 32, "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";}
版主大人 您写的和我2楼一样 解决不了1.120.0.0 1.159.255.255 这个IP区间 生成CIDR的问题
虽然拆分并不是很困难,但你如何确定拆分点呢?
比如
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
所以那个第二种拆法是可以机器实现的,而第一种似只能手工实现
虽然拆分并不是很困难,但你如何确定拆分点呢?
比如
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
所以那个第二种拆法是可以机器实现的,而第一种似只能手工实现
ip2cidr("1.40.0.0","1.44.255.255");
00000000000001001111111111111111
echo long2ip(bindec('111111111111111111111')+ip2long('1.40.0.0')); //1.71.255.255 超出了指定IP的范围
0000000000000100 1111111111111111
要这样取
拆分理应从小区域开始
上来就是一大块,肯定是不合适的
谢谢您 我摸索下
把问题多分析一下
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/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
谢谢版主大人 尤其认识到自身不足 还要好好努力啊

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
