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.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Master IP Address Manipulation in PHP with s1lentium/iptools

This document describes s1lentium/iptools , a PHP library for handling IPv4 and IPv6 addresses.

Installation

composer require s1lentium/iptools

IP Operations

Creating an IP object

$ip = new IP('192.168.1.1');
echo $ip->version; // IPv4

$ip = new IP('fc00::');
echo $ip->version; // IPv6

Parsing 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.1

Conversion 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; // 2130706433

Additional 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.arpa

Network 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/32

Excluding 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/24

Iterating 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')); // true

Iterating 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')); // 254
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

IPv6networkPHPIPv4IP addressrangeCIDR
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.