PHPackages                             h3mantd/iptools - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. h3mantd/iptools

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

h3mantd/iptools
===============

PHP Library for manipulating network addresses (IPv4 and IPv6)

v2.0.0(3mo ago)03MITPHPPHP ^8.2CI passing

Since Feb 14Pushed 3mo agoCompare

[ Source](https://github.com/h3mantD/IPTools)[ Packagist](https://packagist.org/packages/h3mantd/iptools)[ RSS](/packages/h3mantd-iptools/feed)WikiDiscussions master Synced today

READMEChangelog (5)Dependencies (26)Versions (9)Used By (0)

IPTools
=======

[](#iptools)

PHP Library for manipulating network addresses (IPv4 and IPv6).

This repository is a fork of [S1lentium/IPTools](https://github.com/S1lentium/IPTools).

[![CI](https://github.com/h3mantD/IPTools/actions/workflows/ci.yml/badge.svg)](https://github.com/h3mantD/IPTools/actions/workflows/ci.yml)[![Code Climate](https://camo.githubusercontent.com/5f3e014e867b2d8406b2eb35441c290086a5331b4746a0eeabd30098de5f9ef5/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f68336d616e74642f4950546f6f6c732f6261646765732f6770612e737667)](https://codeclimate.com/github/h3mantd/IPTools)

[![PHP 8.2](https://camo.githubusercontent.com/fce8bd6a62dd9ff632f64878eaa63543edf04354cba9912029c9986e2c05a59b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e322d3838393242462e737667)](http://php.net)[![PHP 8.3](https://camo.githubusercontent.com/56ff04428721dbd01eb5f19065130d6482ca9dffc98526e02e35d4846ecf08fd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e332d3838393242462e737667)](http://php.net)[![PHP 8.4](https://camo.githubusercontent.com/4521de29291641d6ce58dbde10672877901c3ea42942780871f81de8d3ba939f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e342d3838393242462e737667)](http://php.net)

Installation
------------

[](#installation)

Requires PHP &gt;= 8.2.

Composer: Run in command line:

```
composer require h3mantd/iptools

```

or put in composer.json:

```
{
    "require": {
        "h3mantd/iptools": "*"
    }
}
```

Usage
-----

[](#usage)

Documentation
-------------

[](#documentation)

- **[Getting Started](docs/getting-started.md)** — Installation and quick examples
- **[Full Documentation](docs/README.md)** — All guides, API reference, and learning path

Key guides: [IP Addresses](docs/ip-addresses.md) | [Networks](docs/networks.md) | [Ranges](docs/ranges.md) | [Range Sets](docs/range-sets.md) | [Parsing](docs/parsing.md) | [Database Storage](docs/database-storage.md) | [Laravel](docs/laravel.md) | [API Reference](docs/api-reference.md)

### IP Operations

[](#ip-operations)

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

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

**Parsing IP from integer, binary and hex:**

```
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
```

or:

```
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
```

**Converting IP 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
echo IP::parse('2001:db8::1')->expanded() // 2001:0db8:0000:0000:0000:0000:0000:0001
```

#### Other public properties:

[](#other-public-properties)

`maxPrefixLength`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:

```
echo IP::parse('192.0.2.5')->reversePointer; // 5.2.0.192.in-addr.arpa
echo IP::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.arpa
```

### IP Type Classification

[](#ip-type-classification)

```
$ip = new IP('127.0.0.1');

echo $ip->primaryType()->value; // loopback
var_dump($ip->isLoopback()); // true
var_dump($ip->isGlobalRoutable()); // false

$types = (new IP('233.252.0.1'))->types();
// MULTICAST + DOCUMENTATION (precedence keeps MULTICAST as primary)
```

### IP Arithmetic and Offsets

[](#ip-arithmetic-and-offsets)

```
$ip = new IP('0.0.0.1');

echo (string) $ip->next(); // 0.0.0.2
echo (string) $ip->previous(); // 0.0.0.0
echo (string) $ip->addOffset(10); // 0.0.0.11
echo (string) $ip->shift(-1); // 0.0.0.2 (left shift by 1)

$distance = (new IP('10.0.0.1'))->distanceTo(new IP('10.0.0.10'));
echo $distance; // 9
```

```
use IPTools\Enums\OverflowMode;

$max = new IP('255.255.255.255');

var_dump($max->next()); // null (boundary-safe convenience)
echo (string) $max->addOffset(1, OverflowMode::WRAP); // 0.0.0.0
echo (string) $max->addOffset(1, OverflowMode::CLAMP); // 255.255.255.255
```

### IPv4 &lt;-&gt; IPv6 Conversions

[](#ipv4---ipv6-conversions)

```
// IPv4-mapped
$mapped = IP::toIpv4Mapped(new IP('127.0.0.1')); // ::ffff:127.0.0.1
echo (string) IP::fromIpv4Mapped($mapped); // 127.0.0.1

// 6to4
$sixToFour = IP::to6to4(new IP('10.0.0.1')); // 2002:a00:1::
echo (string) IP::from6to4($sixToFour); // 10.0.0.1

// NAT64 /96 (default 64:ff9b::/96)
$nat64 = IP::toNat64(new IP('8.8.8.8')); // 64:ff9b::808:808
echo (string) IP::fromNat64($nat64); // 8.8.8.8
```

### Flexible Parsing

[](#flexible-parsing)

```
use IPTools\ParseFlags;
use IPTools\Parser;

$parsed = Parser::ip('[2001:db8::1]:443');
echo (string) $parsed->ip; // 2001:db8::1
echo $parsed->port; // 443

$zoned = Parser::ip('fe80::1%eth0');
echo $zoned->zoneId; // eth0

echo (string) Parser::ip('0x0a000001')->ip; // 10.0.0.1
echo (string) Parser::range('192.168.*.*'); // 192.168.0.0/16

Parser::ip('1.2.3.4:80', ParseFlags::STRICT); // throws in strict mode
```

### Database-Backed Range Lookup

[](#database-backed-range-lookup)

This module is optional. If you do not need database-backed lookups, you can skip this entire section and continue using the in-memory IP/Network/Range features.

```
use IPTools\IP;
use IPTools\Network;
use IPTools\Storage\SqlRangeStorage;

$pdo = new PDO('sqlite::memory:');
$pdo->exec('CREATE TABLE ip_ranges (id INTEGER PRIMARY KEY AUTOINCREMENT, version INTEGER NOT NULL, start_bin BLOB NOT NULL, end_bin BLOB NOT NULL, metadata TEXT NULL)');

$storage = new SqlRangeStorage($pdo);
$storage->store(Network::parse('192.0.2.0/24'), ['source' => 'docs']);

var_dump($storage->contains(new IP('192.0.2.10'))); // true

foreach ($storage->findContaining(new IP('192.0.2.10')) as $match) {
    echo $match['range']->getFirstIP() . '-' . $match['range']->getLastIP() . PHP_EOL;
    var_dump($match['metadata']);
}
```

MySQL schema:

```
CREATE TABLE ip_ranges (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  version TINYINT NOT NULL,
  start_bin BINARY(16) NOT NULL,
  end_bin   BINARY(16) NOT NULL,
  metadata  JSON NULL,
  KEY idx_lookup (version, start_bin, end_bin)
) ENGINE=InnoDB;
```

PostgreSQL schema:

```
CREATE TABLE ip_ranges (
  id BIGSERIAL PRIMARY KEY,
  version SMALLINT NOT NULL,
  start_bin BYTEA NOT NULL,
  end_bin   BYTEA NOT NULL,
  metadata  JSONB NULL
);

CREATE INDEX idx_lookup ON ip_ranges (version, start_bin, end_bin);
```

### Laravel Integration

[](#laravel-integration)

Laravel integration is optional and built around `RangeStorageInterface`.

If package discovery is enabled, `IPTools\IPToolsServiceProvider` is registered automatically.

Install package assets with the installer command:

```
php artisan iptools:install
```

Or publish assets manually:

```
php artisan vendor:publish --tag=iptools-config
php artisan vendor:publish --tag=iptools-migrations
php artisan vendor:publish --tag=iptools-model
php artisan migrate
```

Resolve storage from the container:

```
use IPTools\IP;
use IPTools\Network;
use IPTools\Storage\RangeStorageInterface;

$storage = app(RangeStorageInterface::class);

$storage->store(Network::parse('10.0.0.0/24'), [
    'policy' => 'allow',
    'source' => 'admin',
]);

var_dump($storage->contains(new IP('10.0.0.42'))); // true
```

You may instantiate the adapter directly when you need explicit control over connection and table name:

```
use Illuminate\Support\Facades\DB;
use IPTools\Storage\LaravelRangeStorage;

$storage = new LaravelRangeStorage(DB::connection(), 'ip_ranges');
```

`findContaining()` yields rows in the following shape:

```
[
    'range' => IPTools\Range,
    'metadata' => array,
]
```

For full Laravel setup details, see [docs/laravel.md](docs/laravel.md).

### Network Operations

[](#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
```

**Convenience helpers:**

```
$network = Network::parse('192.0.2.130/24');

echo (string) $network->networkAddress(); // 192.0.2.0
echo (string) $network->broadcastAddress(); // 192.0.2.255
echo (string) $network->firstHost(); // 192.0.2.1
echo (string) $network->lastHost(); // 192.0.2.254
echo $network->usableHostCount(); // 254

var_dump($network->containsIP('192.0.2.42')); // true
var_dump($network->containsRange('192.0.2.10-192.0.2.20')); // true

echo (string) $network->nextSubnet(); // 192.0.3.0/24
echo (string) $network->previousSubnet(); // 192.0.1.0/24
```

Point-to-point behavior:

```
$v4p2p = Network::parse('198.51.100.0/31');
var_dump($v4p2p->isPointToPoint()); // true
echo $v4p2p->usableHostCount(); // 2

$v6p2p = Network::parse('2001:db8::/127');
var_dump($v6p2p->isPointToPoint()); // true
```

**Exclude IP from Network:**

```
$excluded = Network::parse('192.0.0.0/8')->exclude(new IP('192.168.1.1'));
foreach($excluded as $network) {
	echo (string)$network . '';
}
```

```
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(new Network('192.168.1.0/24'));
foreach($excluded as $network) {
	echo (string)$network . '';
}
```

```
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 ($networks as $network) {
	echo (string)$network . '';
}
```

```
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($network as $ip) {
	echo (string)$ip . '';
}
```

```
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($hosts as $ip) {
	echo (string)$ip . '';
}
```

```
192.168.1.1
...
192.168.1.254

```

**Count Network IP adresses**

```
echo count(Network::parse('192.168.1.0/24')) // 254
```

**Count very large Networks precisely (IPv6):**

```
echo Network::parse('2001:db8::/64')->getCountPrecise(); // 18446744073709551616
```

**Summarize adjacent/redundant networks:**

```
$summary = Network::summarize([
    '10.0.0.0/24',
    '10.0.1.0/24',
    '10.0.0.0/25',
]);

foreach ($summary as $network) {
    echo (string) $network . PHP_EOL;
}
// 10.0.0.0/23
```

### Range Operations

[](#range-operations)

**Define the range in different formats:**

```
$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');
```

**Check if IP is within Range:**

```
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
```

**Iterate over Range IP adresses:**

```
$range = Range::parse('192.168.1.1-192.168.1.254');
foreach($range as $ip) {
	echo (string)$ip . '';
}
```

```
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($networks as $network) {
	echo (string)$network . '';
}
```

```
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**

```
echo count(Range::parse('192.168.1.1-192.168.1.254')) // 254
```

**Count very large Ranges precisely (IPv6):**

```
echo Range::parse('2001:db8::/64')->getCountPrecise(); // 18446744073709551616
```

**Lazy network decomposition helpers:**

```
$range = Range::parse('49.12.11.10-49.12.11.35');

echo (string) $range->getFirstNetwork(); // 49.12.11.10/31
echo (string) $range->getLastNetwork();  // 49.12.11.32/30
echo (string) $range->getNthNetwork(2);  // 49.12.11.16/28

foreach ($range->iterateNetworks() as $network) {
    echo (string) $network . PHP_EOL;
}
```

### RangeSet Algebra

[](#rangeset-algebra)

```
use IPTools\IP;
use IPTools\RangeSet;

$set = new RangeSet([
    '10.0.0.0-10.0.0.10',
    '10.0.0.11-10.0.0.20',
]);

// Canonicalized automatically: adjacent ranges are merged
var_dump(count($set)); // 1

$other = new RangeSet(['10.0.0.5-10.0.0.15']);

$intersection = $set->intersect($other);
$difference = $set->subtract($other);
$union = $set->union('10.0.0.50-10.0.0.60');

var_dump($set->contains(new IP('10.0.0.8'))); // true
var_dump($set->containsRange('10.0.0.1-10.0.0.3')); // true
var_dump($set->overlaps('10.0.0.18-10.0.1.1')); // true

foreach ($difference->toCidrs() as $network) {
    echo (string) $network . PHP_EOL;
}
```

License
=======

[](#license)

The library is released under the [MIT](https://opensource.org/licenses/MIT).

###  Health Score

39

—

LowBetter than 84% of packages

Maintenance82

Actively maintained with recent releases

Popularity3

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~12 days

Total

5

Last Release

90d ago

Major Versions

v1.3.0 → v2.0.02026-04-05

### Community

Maintainers

![](https://www.gravatar.com/avatar/bcc823556b7bd207cccacedb622aa8da3f6bc568e24a2223ffccbc60fc6fafc6?d=identicon)[h3mantd](/maintainers/h3mantd)

---

Top Contributors

[![S1lentium](https://avatars.githubusercontent.com/u/2018277?v=4)](https://github.com/S1lentium "S1lentium (62 commits)")[![h3mantD](https://avatars.githubusercontent.com/u/50235905?v=4)](https://github.com/h3mantD "h3mantD (57 commits)")[![grongor](https://avatars.githubusercontent.com/u/972493?v=4)](https://github.com/grongor "grongor (6 commits)")[![bartvanhoutte](https://avatars.githubusercontent.com/u/4867154?v=4)](https://github.com/bartvanhoutte "bartvanhoutte (3 commits)")[![petski](https://avatars.githubusercontent.com/u/116610?v=4)](https://github.com/petski "petski (2 commits)")[![digibeuk](https://avatars.githubusercontent.com/u/5148394?v=4)](https://github.com/digibeuk "digibeuk (2 commits)")[![jippi](https://avatars.githubusercontent.com/u/22841?v=4)](https://github.com/jippi "jippi (1 commits)")[![eugenekkh](https://avatars.githubusercontent.com/u/8081158?v=4)](https://github.com/eugenekkh "eugenekkh (1 commits)")[![clayfreeman](https://avatars.githubusercontent.com/u/182658?v=4)](https://github.com/clayfreeman "clayfreeman (1 commits)")[![simPod](https://avatars.githubusercontent.com/u/327717?v=4)](https://github.com/simPod "simPod (1 commits)")[![wdjwxh](https://avatars.githubusercontent.com/u/3990956?v=4)](https://github.com/wdjwxh "wdjwxh (1 commits)")

---

Tags

ipv6IPnetworkipv4cidrsubnetIP-Tools

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/h3mantd-iptools/health.svg)

```
[![Health](https://phpackages.com/badges/h3mantd-iptools/health.svg)](https://phpackages.com/packages/h3mantd-iptools)
```

###  Alternatives

[s1lentium/iptools

PHP Library for manipulating network addresses (IPv4 and IPv6)

2446.6M28](/packages/s1lentium-iptools)[mlocati/ip-lib

Handle IPv4, IPv6 addresses and ranges

3167.3M70](/packages/mlocati-ip-lib)[markrogoyski/ipv4-subnet-calculator

Network calculator for subnet mask and other classless (CIDR) network information.

175852.6k14](/packages/markrogoyski-ipv4-subnet-calculator)[zoujingli/ip2region

ip2region v3.0 for PHP - 企业级 IP 地理位置查询库，支持 IPv4 和 IPv6，多种缓存策略，零依赖，开箱即用

1.4k475.0k67](/packages/zoujingli-ip2region)[longman/ip-tools

PHP IP Tools for manipulation with IPv4 and IPv6

147256.8k7](/packages/longman-ip-tools)[rlanvin/php-ip

IPv4/IPv6 manipulation library for PHP

180803.8k12](/packages/rlanvin-php-ip)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
