PHP Library for manipulating network addresses (IPv4 and IPv6).
composer require cdn77/iptools
$ip = newIP('192.168.1.1');
echo$ip->version;// IPv4$ip = newIP('fc00::');
echo$ip->version; // IPv6Parsing IP from integer, binary and hex:
echo (string)IP::parse(2130706433); // 127.0.0.1echo (string)IP::parse('0b11000000101010000000000100000001') // 192.168.1.1
echo (string)IP::parse('0x0a000001'); // 10.0.0.1or:
echo (string)IP::parseLong(2130706433); // 127.0.0.1echo (string)IP::parseBin('11000000101010000000000100000001'); // 192.168.1.1echo (string)IP::parseHex('0a000001'); // 10.0.0.1Converting IP to other formats:
echoIP::parse('192.168.1.1')->bin// 11000000101010000000000100000001
echo IP::parse('10.0.0.1')->hex// 0a000001
echo IP::parse('127.0.0.1')->long// 2130706433maxPrefixLength
The max number of bits in the address representation: 32 for IPv4, 128 for IPv6.
octetsCount
The count of octets in IP address: 4 for IPv4, 16 for IPv6
reversePointer
The name of the reverse DNS PTR for the address:
echonewIP::parse('192.0.2.5')->reversePointer // 5.2.0.192.in-addr.arpa
echo newIP::parse('2001:db8::567:89ab')->reversePointer// b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpaecho Network::parse('192.0.0.1 255.0.0.0')->CIDR; // 192.0.0.0/8echo (string)Network::parse('192.0.0.1/8')->netmask; // 255.0.0.0echo (string)Network::parse('192.0.0.1'); // 192.0.0.1/32Exclude IP from Network:
$excluded = Network::parse('192.0.0.0/8')->exclude(newIP('192.168.1.1'));
foreach($excludedas$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
Exclude Subnet from Network:
$excluded = Network::parse('192.0.0.0/8')->exclude(newNetwork('192.168.1.0/24'));
foreach($excludedas$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
Split network into equal subnets
$networks = Network::parse('192.168.0.0/22')->moveTo('24');
foreach ($networksas$network) {
echo (string)$network . '<br>';
}192.168.0.0/24
192.168.1.0/24
192.168.2.0/24
192.168.3.0/24
Iterate over Network IP adresses:
$network = Network::parse('192.168.1.0/24');
foreach($networkas$ip) {
echo (string)$ip . '<br>';
}192.168.1.0
...
192.168.1.255
Get Network hosts adresses as Range:
$hosts = Network::parse('192.168.1.0/24')->hosts // Range(192.168.1.1, 192.168.1.254);foreach($hostsas$ip) {
echo (string)$ip . '<br>';
}192.168.1.1
...
192.168.1.254
Count Network IP adresses
echocount(Network::parse('192.168.1.0/24')) // 254Define the range in different formats:
$range = newRange(newIP('192.168.1.0'), newIP('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');Check if IP is within Range:
echo Range::parse('192.168.1.1-192.168.1.254')->contains(newIP('192.168.1.5')); // trueecho Range::parse('::1-::ffff')->contains(newIP('::1234')); // trueIterate over Range IP adresses:
$range = Range::parse('192.168.1.1-192.168.1.254');
foreach($rangeas$ip) {
echo (string)$ip . '<br>';
}192.168.1.1
...
192.168.1.254
Get Networks that fit into a specified range of IP Adresses:
$networks = Range::parse('192.168.1.1-192.168.1.254')->getNetworks();
foreach($networksas$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
Count IP adresses in Range
echocount(Range::parse('192.168.1.1-192.168.1.254')) // 254The library is released under the MIT.