olragon / iptools
用于操作网络地址(IPv4和IPv6)的PHP库
dev-master
2014-05-22 08:50 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-24 07:27:15 UTC
README
IP操作
$ip = new IP('192.168.1.1'); echo $ip->version;// IPv4
$ip = new IP('fc00::'); echo $ip->version; // IPv6
从整数、二进制和十六进制表示中解析IP
echo (string)IP::parse(2130706433); // 127.0.0.1 echo (string)IP::parse('0b11000000101010000000000100000001') // 192.168.1.1 echo (string)IP::parse('0x0a000001'); // 10.0.0.1
或
echo (string)IP::parseLong(2130706433); // 127.0.0.1 echo (string)IP::parseBin('11000000101010000000000100000001'); // 192.168.1.1 echo (string)IP::parseHex('0a000001'); // 10.0.0.1
将IP转换为其他格式
echo IP::parse('192.168.1.1')->bin // 11000000101010000000000100000001 echo IP::parse('10.0.0.1')->hex // 0a000001 echo IP::parse('127.0.0.1')->long // 2130706433
网络
$network = new Network(new IP('192.168.1.1'), new IP('255.255.255.0')); print_r($network->info);
Array
(
[IP] => 192.168.1.1
[netmask] => 255.255.255.0
[network] => 192.168.1.0
[prefixLength] => 24
[CIDR] => 192.168.1.0/24
[wildcard] => 0.0.0.255
[broadcast] => 192.168.1.255
[firstIP] => 192.168.1.0
[lastIP] => 192.168.1.255
[blockSize] => 256
[firstHost] => 192.168.1.1
[lastHost] => 192.168.1.254
[hostsCount] => 254
)
echo Network::parse('192.0.0.1 255.0.0.0')->CIDR; // 192.0.0.0/8 echo (string)Network::parse('192.0.0.1/8')->netmask; // 255.0.0.0 echo (string)Network::parse('192.0.0.1'); // 192.0.0.1/32
迭代网络的宿主IP
$network = Network::parse('192.168.1.0/24'); foreach($network as $ip) { echo (string)$ip . '<br>'; }
192.168.1.1
...
192.168.1.254
排除网络中的IP
$excluded = Network::parse('192.0.0.0/8')->exclude(new IP('192.168.1.1')); foreach($excluded as $network) { echo (string)$network . '<br>'; }
192.0.0.0/9
192.128.0.0/11
192.160.0.0/13
192.168.0.0/24
192.168.1.0/32
192.168.1.2/31
...
192.192.0.0/10
排除网络中的子网
$excluded = Network::parse('192.0.0.0/8')->exclude(new Network('192.168.1.0/24')); foreach($excluded as $network) { echo (string)$network . '<br>'; }
192.0.0.0/9
192.128.0.0/11
192.160.0.0/13
192.168.0.0/24
192.168.2.0/23
...
192.192.0.0/10
宿主IP数量
echo count(Network::parse('192.168.1.0/24')) // 254
范围操作
以不同格式定义范围
$range = new Range(new IP('192.168.1.0'), new IP('192.168.1.255')); $range = Range::parse('192.168.1.0-192.168.1.255'); $range = Range::parse('192.168.1.*'); $range = Range::parse('192.168.1.0/24');
检查IP是否在范围内
echo Range::parse('192.168.1.1-192.168.1.254')->contains(new IP('192.168.1.5')); // true echo Range::parse('::1-::ffff')->contains(new IP('::1234')); // true
迭代范围IP
$range = Range::parse('192.168.1.1-192.168.1.254'); foreach($range as $ip) { echo (string)$ip . '<br>'; }
192.168.1.1
...
192.168.1.254
获取适合指定IP范围的网络
$networks = Range::parse('192.168.1.1-192.168.1.254')->getNetworks(); foreach($networks as $network) { echo (string)$network . '<br>'; }
192.168.1.1/32
192.168.1.2/31
192.168.1.4/30
192.168.1.8/29
192.168.1.16/28
192.168.1.32/27
192.168.1.64/26
192.168.1.128/26
192.168.1.192/27
192.168.1.224/28
192.168.1.240/29
192.168.1.248/30
192.168.1.252/31
192.168.1.254/32
范围中IP数量
echo count(Range::parse('192.168.1.1-192.168.1.254')) // 254