Master IP Address Manipulation in PHP with s1lentium/iptools
This guide explains how to install the s1lentium/iptools PHP library and use its classes to parse, convert, and operate on IPv4 and IPv6 addresses, networks, and ranges, including examples of CIDR calculations, reverse DNS, and host enumeration.
This document describes s1lentium/iptools , a PHP library for handling IPv4 and IPv6 addresses.
Installation
composer require s1lentium/iptoolsIP Operations
Creating an IP object
$ip = new IP('192.168.1.1');
echo $ip->version; // IPv4
$ip = new IP('fc00::');
echo $ip->version; // IPv6Parsing from integer, binary or hexadecimal
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
// helper methods
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.1Conversion to other formats
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; // 2130706433Additional IP properties
maxPrefixLength– maximum prefix length (IPv4 = 32, IPv6 = 128) octetsCount – number of octets (IPv4 = 4, IPv6 = 16) reversePointer – reverse DNS PTR name for the address
echo (new IP('192.0.2.5'))->reversePointer; // 5.2.0.192.in-addr.arpa
echo (new IP('2001:db8::567:89ab'))->reversePointer; // b.a.9.8.7.6.5...ip6.arpaNetwork Operations
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/32Excluding an IP from a network
$excluded = Network::parse('192.0.0.0/8')->exclude(new IP('192.168.1.1'));
foreach ($excluded as $network) {
echo (string)$network . "
";
}Splitting a network into equal sub‑nets
$networks = Network::parse('192.168.0.0/22')->moveTo('24');
foreach ($networks as $network) {
echo (string)$network . "
";
}
// Result: 192.168.0.0/24, 192.168.1.0/24, 192.168.2.0/24, 192.168.3.0/24Iterating all IPs in a network
$network = Network::parse('192.168.1.0/24');
foreach ($network as $ip) {
echo (string)$ip . "
";
}Enumerating host range (excluding network and broadcast)
$hosts = Network::parse('192.168.1.0/24')->hosts; // Range(192.168.1.1, 192.168.1.254)
foreach ($hosts as $ip) {
echo (string)$ip . "
";
}Range Operations
Defining a range
$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');Containment check
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')); // trueIterating over a range
$range = Range::parse('192.168.1.1-192.168.1.254');
foreach ($range as $ip) {
echo (string)$ip . "
";
}Deriving minimal networks that cover a range
$networks = Range::parse('192.168.1.1-192.168.1.254')->getNetworks();
foreach ($networks as $network) {
echo (string)$network . "
";
}Counting addresses in a range
echo count(Range::parse('192.168.1.1-192.168.1.254')); // 254Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
