PHPackages                             matracine/ip-tools - 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. matracine/ip-tools

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

matracine/ip-tools
==================

IP address handling library

v1.1(2y ago)32.8k↓33.3%1MITPHPPHP ^7.2|^8.0

Since Dec 30Pushed 2y ago1 watchersCompare

[ Source](https://github.com/matracine/ip-tools)[ Packagist](https://packagist.org/packages/matracine/ip-tools)[ RSS](/packages/matracine-ip-tools/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (15)Used By (1)

IPTOOLS
=======

[](#iptools)

PHP Library for manipulating network addresses.

This library implements tools for IP network address manipulations. It actualy impelments IPv4 only classes, but IPv6 will come. It has no dependencies others than PHP 7.0.

Classes :

- IPv4\\Address
- IPv4\\Network
- IPv4\\Range
- IPv4\\Subnet

Please feal free to open issue for improvements, bugs, requests... IPv6 implementation is on the way.

Docblock documentation implemented.

QA
--

[](#qa)

ServiceResult**Travis CI** (PHP 7.0 + 7.1 + 7.2)[![Build Status](https://camo.githubusercontent.com/d4a3ccc50e4bd2a3fdd9cd80d640bff08973f74fd3892644a556f41e1e0856c9/68747470733a2f2f7472617669732d63692e6f72672f6d6174726163696e652f69702d746f6f6c732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/matracine/ip-tools)**Scrutinizer score**[![Scrutinizer Code Quality](https://camo.githubusercontent.com/fa63b9af5769947184af6a07f97965682b079caa70db388595729cddafac837a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d6174726163696e652f69702d746f6f6c732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/matracine/ip-tools/?branch=master)**Code coverage**[![Code Coverage](https://camo.githubusercontent.com/0420602e03ecc26433cc311e88f579dfcf383f6b3c9151eb1591d08335929419/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d6174726163696e652f69702d746f6f6c732f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/matracine/ip-tools/?branch=master)Installation
------------

[](#installation)

With composer:

```
composer require matracine/ip-tools

```

Usage
-----

[](#usage)

### IPv4\\Address

[](#ipv4address)

Class for IPv4 Address manipulation.

An IP address is no more than a 32 bits integer. To be more human readable, the dot quad string notation is commonly used (ex : "123.235.123.213").
Class is immutable : once created cennot be modified. Opérations on an existing instance will return a new instance.

#### Namespace :

[](#namespace-)

```
use mracine\IPTools\IPv4\Address;
```

#### Create an IPv4 Address :

[](#create-an-ipv4-address-)

Constructor use integer

```
$address = new Address(0xff00ff00); // 255.0.255.0
```

Helpers

```
$address = Address::fromString('10.11.12.13');   // From a dot quad string notation

$address = Address::fromArray([10, 11, 12, 13]); // From an Array of integers
$address = Address::fromArray(['10', '11', '12', '13']); // From an Array  of strings
```

#### Retreive value

[](#retreive-value)

```
// Get a string with dot quad notation
$value = (string)$address;
$value = $address->asDotQuad();
// returns "1.2.3.4"

// Get the integer representation of address
$value = $address->asInteger();
$value = $address->int();
```

#### Operations

[](#operations)

```
// get an addresse from another shifted by un offset (negateive or positive)
$address2 = $address->shift(10);  // 10.0.0.0 => 10.0.0.10
$address2 = $address->shift(-10); // 10.0.0.0 => 9.255.255.246

// get next address
$address2 = $address->next();  // 10.0.0.0 => 10.0.0.1

// get previous address
$address2 = $address->previous();  // 10.0.0.0 => 9.255.255.255
```

#### Qualify

[](#qualify)

```
// Test if two addresses are equivalent (same value)
if ($address->match($address2))
{...}

// Test if an address is in Range or Subnet
if($address->isIn($range))
{...}

// Address is part of RFC 1918 subnets ?
// see : https://tools.ietf.org/html/rfc1918
if($address->isRFC1918())
{...}

// Address is part of RFC 6598 subnet ?
// see : https://tools.ietf.org/html/rfc6598
if($address->isRFC6598())
{...}

// Address is multicast ?
// see : https://tools.ietf.org/html/rfc1112
if($address->isMulticast())
{...}

// get the CLASS of an address
// Obsolete notion for class routing
switch($addres->getClass())
{
    case Address::CLASS_A:
        ...
        break;
    case Address::CLASS_B:
        ...
        break;
    case Address::CLASS_C:
        ...²
        break;
    case Address::CLASS_D:
        ...
        break;
    case Address::CLASS_E:
        ...
        break;
}
```

#### TODO

[](#todo)

- Add string formater

### IPv4\\Netmask

[](#ipv4netmask)

Class for IPv4 Netmask manipulation.

A Netmask is an IP Address (Netmask extends Address) with only special values allowed (255.255.255.0, 255.0.0.0 etc.). In fact only 33 values are allowed... All Address methods are usable with Netmask, even if certains are meaningless (isRFC1918, getClass...).

#### Namespace :

[](#namespace--1)

```
use mracine\IPTools\IPv4\Netmask;
```

#### Create an IPv4 Netmask :

[](#create-an-ipv4-netmask-)

Constructor use integer

```
$netmask = new Netmask(0xffffff00); // 255.255.255.0

// trying to constrct a netmask with wrong value will throw an exception (\DomainException)
$netmask = new Netmask(0x12345678); // throws \DomainException
```

Helpers

Netmask extends Address, so Address helpers are availables. Check are done, Exceptions are thrown when values cannot be used to create valid Netmask.

```
// Construct a Netmask from a CIDR notation
$netmask = Netmask::fromCidr(24); // '255.255.255.0'
$netmask = Netmask::fromCidr(-1); // throws \OutOfBoundsException
$netmask = Netmask::fromCidr(33); // throws \OutOfBoundsException

// Construct a Netmask from an Address
$netmask = Netmask::fromAddress(new Adress::fromString('255.255.255.0')); // 255.255.255.0

// In fact, Netmask extends Address, so you can use Address Helpers
$netmask = Netmask::fromString('255.255.255.0'); // 255.255.255.0
$netmask = Netmask::fromString('255.0.255.0'); // throws \DomainException, invalid netmask...
```

#### Retreive value

[](#retreive-value-1)

Use Address methods plus

```
// Get the CIDR representation of Netmask (integer between 0 to 32
$cidr = $netmask->asCidr();

// Count the number of addresses in a netmask
// Imple!ents Countable
$count = count(Netmask::fromString('255.255.255.0')); // 256

$count = Netmask::fromString('255.255.255.0')->count(); // 256
```

#### Operations

[](#operations-1)

Address methods are modified to correspond to the Netmask logic. When shifting a Netmask, we increment or decrement CIDR vaue by the offset.

```
// get a Netmask from another shifted by un offset (negateive or positive)
$netmask2 = $netmask->shift(1);  // 255.255.255.0 => 255.255.255.128
$netmask2 = $netmask->shift(-1); // 255.255.255.0 => 255.255.254.0

// get next netmask
$netmask2 = $netmask->next();  // 255.255.255.0 => 255.255.255.128

// get previous netmask
$netmask = $netmask->previous();  // // 255.255.255.0 => 255.255.254.0
```

### IPv4\\Range

[](#ipv4range)

Class for IPv4 Ranges manipulation.

A Range of IP Addresses is consecutives IP addresses, determined by a lower and a upper boundaries. Class is immutable : once created cannot be modified. Opérations on an existing instance will return a new range instance or a fresh Address instance.

#### Namespace :

[](#namespace--2)

```
use mracine\IPTools\IPv4\Range;
```

#### Create an IPv4 Range :

[](#create-an-ipv4-range-)

Constructor use an lower bound Address and a upper bound Address. Arrange bounds, no need to passer lower bound first

```
$range = new Range(new Address('192.168.1.5'), new Address('192.168.1.34'));
$range = new Range(new Address('192.168.1.34'), new Address('192.168.1.5'));
```

Helpers

```
$range = Range::fromCount(new Address('192.168.1.5'), 10); // From a lower bound, 10 addresses : 192.168.1.5-192.168.1.14
$range = Range::fromCount(new Address('192.168.1.5'), -5); // From a uppen bound, 10 addresses : 192.168.0.252-192.168.1.5
```

#### Retreive values

[](#retreive-values)

```
// Retreive the first Address
$address = $range->getUpperBound();

// Retreive the last Address
$address = $range->getLowerBound();

// Implements ArrayAccess
$range = new Range(new Address('192.168.1.5'), new Address('192.168.1.10'));   // Same as constructor
$address = $range[0]; // 192.168.1.5
$address = $range[2]; // 192.168.1.7

// Trying to access an address out of the range will raise an exception
$address = $range[-1]; // exception
$address = $range[50]; // exception

// Read only : Trying to affect or unset an item will raise an exception
$range[1] = $address; // exception
unset($range[1]); // exception
```

#### Operations

[](#operations-2)

```
// get a range from another shifted by an offset (negateive or positive), keeping the same address count
$range2 = $range1->shift(2);  // 192.168.1.10-192.168.1.15 => 192.168.1.27-192.168.1.32
$range2 = $range1->shift(-1); // 192.168.1.10-192.168.1.15 => 192.168.1.9-192.168.1.4

// get next adjacent range, with same address count
$range2 = $range->next();  // 192.168.1.0-192.168.1.9 => 192.168.1.10-192.168.1.19

// get previous adjacent range, with same address count
$range2 = $range->previous();  // 192.168.1.0-192.168.1.9 => 192.168.0.246-192.168.0.255

// Iterate all the addresses of the range
foreach($subnet as $address)
{...}
```

#### Qualify

[](#qualify-1)

```
// true if 2 ranges are equivalent (same pper and lower bounds)
if($range->match($range2))
{...}

// Check if an Address is contained in a range
if($range->contains($address))
{...}

// Check if a range is contained in an other range
if($range->isIn($range2))
{...}

// Get count of addresses in a range
$addressCount = count($range); // Interface Countable
$addressCount = $range->count();
```

### IPv4\\Subnet

[](#ipv4subnet)

Class for IPv4 Subnets manipulation.

A Subnet is a range (Subnet extends Range) of IP Addresses, determined by a network address and a netmask. Class is immutable : once created cennot be modified. Opérations on an existing instance will return a new subnet instance or a fresh Address instance.

#### Namespace :

[](#namespace--3)

```
use mracine\IPTools\IPv4\Subnet;
```

#### Create an IPv4 Subnet :

[](#create-an-ipv4-subnet-)

Constructor use an Address (network address) and Netmask.

```
$subnet = new Subnet(new Address('192.168.1.0'), new Netmask('255.255.255.250'));
```

By default, strict network/netmask validity checking is enabled. You can specify third optional parameter (strict) to false to enable intelligent network address calculation, and provide an address within the subnet.

```
$subnet = new Subnet(new Address('192.168.1.50'), new Netmask('255.255.255.250'), false);  // network address will be calculated to 192.168.1.0
```

Helpers

```
// Creates a subnet from CIDR notation
$subnet = Subnet::fromCidr(new Address('192.168.1.0'), 24);

// Creates a subnet from a string formated with CIDR or address/netmask notation
$subnet = Subnet::fromString('192.168.1.0/24');
$subnet = Subnet::fromString('192.168.1.0/255.255.255.0');
```

#### Retreive values

[](#retreive-values-1)

Range methods plus :

```
// Retreive the network as an Address object
$address = $ubnet->getNetworkAddress(); // 192.168.1.0

// Retreive the netmask as a Netmask object
$netmask = $ubnet->getNetmaskAddress(); // 255.25.255.0

// Retreive the broadcast address as an Address object
$address = $ubnet->getBroadcastAddress(); // 192.168.1.255
```

#### Operations

[](#operations-3)

Same as Range methods...

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity73

Established project with proven stability

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 ~137 days

Recently: every ~402 days

Total

13

Last Release

1046d ago

PHP version history (2 changes)v1.0.0-RC1PHP ^7.0

v1.1PHP ^7.2|^8.0

### Community

Maintainers

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

---

Tags

utilityIPnetwork

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/matracine-ip-tools/health.svg)

```
[![Health](https://phpackages.com/badges/matracine-ip-tools/health.svg)](https://phpackages.com/packages/matracine-ip-tools)
```

###  Alternatives

[nette/utils

🛠 Nette Utils: lightweight utilities for string &amp; array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.

2.1k394.3M1.5k](/packages/nette-utils)[s1lentium/iptools

PHP Library for manipulating network addresses (IPv4 and IPv6)

2446.2M24](/packages/s1lentium-iptools)[markrogoyski/ipv4-subnet-calculator

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

177813.7k6](/packages/markrogoyski-ipv4-subnet-calculator)[phpcsstandards/phpcsutils

A suite of utility functions for use with PHP\_CodeSniffer

6333.4M58](/packages/phpcsstandards-phpcsutils)[jaaulde/php-ipv4

PHP classes for working with IPV4 addresses and networks.

1034.6k](/packages/jaaulde-php-ipv4)

PHPackages © 2026

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