PHPackages                             detain/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. detain/iptools

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

detain/iptools
==============

PHP Library for manipulating network addresses (IPv4 and IPv6)

v3.0.3(6y ago)27.9k↓49%1[1 PRs](https://github.com/detain/iptools/pulls)MITPHPPHP &gt;=5.6.0CI passing

Since Jul 27Pushed 3y agoCompare

[ Source](https://github.com/detain/iptools)[ Packagist](https://packagist.org/packages/detain/iptools)[ RSS](/packages/detain-iptools/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (4)Dependencies (3)Versions (11)Used By (0)

IPTools
=======

[](#iptools)

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

[![Build Status](https://camo.githubusercontent.com/3999b73b8ac13b4849ed2d1e8e83f7b160648ad6b01a822445892dcf7d2c76d2/68747470733a2f2f7472617669732d63692e6f72672f64657461696e2f6970746f6f6c732e737667)](https://travis-ci.org/detain/iptools)[![Coverage Status](https://camo.githubusercontent.com/daffe5c9870c1c82a34b4b9152dbf2bacdef1046ba5736531937cb91d8bdd152/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f64657461696e2f6970746f6f6c732f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/detain/iptools?branch=master)[![Code Climate](https://camo.githubusercontent.com/c9d66d20ab8c0ed3fa3bc736e49927bc41ec7f30c350b5c30587a46259fe9ac6/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f64657461696e2f6970746f6f6c732f6261646765732f6770612e737667)](https://codeclimate.com/github/detain/iptools)

[![Latest Stable Version](https://camo.githubusercontent.com/32f8b32a4d0da4dce8d93d603b40b52996a7800ec52b7f35ad17e1f5c1bd9054/68747470733a2f2f706f7365722e707567782e6f72672f64657461696e2f6970746f6f6c732f76657273696f6e)](https://packagist.org/packages/detain/iptools)[![Total Downloads](https://camo.githubusercontent.com/439dbc32b049c82785bffb296084bbd607fc2ce940d17b5015e8a7e433139f83/68747470733a2f2f706f7365722e707567782e6f72672f64657461696e2f6970746f6f6c732f646f776e6c6f616473)](https://packagist.org/packages/detain/iptools)[![Latest Unstable Version](https://camo.githubusercontent.com/7d32cb12ac0c7460c19f1577357306c3c162d7bc5a58a698af33d3ab5d4b3157/68747470733a2f2f706f7365722e707567782e6f72672f64657461696e2f6970746f6f6c732f762f756e737461626c65)](//packagist.org/packages/detain/iptools)[![License](https://camo.githubusercontent.com/bbc37a7b1bae9724f706a9d64daec1a6a9fae23535aa3ac58608c15efbee8c27/68747470733a2f2f706f7365722e707567782e6f72672f64657461696e2f6970746f6f6c732f6c6963656e7365)](https://packagist.org/packages/detain/iptools)[![Monthly Downloads](https://camo.githubusercontent.com/ae415469bb8fcac39eed41c651e2d6bb6f88e9deda9c207fdda13c418c2db5cf/68747470733a2f2f706f7365722e707567782e6f72672f64657461696e2f6970746f6f6c732f642f6d6f6e74686c79)](https://packagist.org/packages/detain/iptools)[![Daily Downloads](https://camo.githubusercontent.com/b905707d5836dc46e48fa4d1a32ef13332216f1ecabbcddc60d60de7b6aa7d64/68747470733a2f2f706f7365722e707567782e6f72672f64657461696e2f6970746f6f6c732f642f6461696c79)](https://packagist.org/packages/detain/iptools)[![composer.lock available](https://camo.githubusercontent.com/53771a348d39ab508f3d2d84b991314cb23f4835b2fa6949fa50f2d30ead5e9f/68747470733a2f2f706f7365722e707567782e6f72672f64657461696e2f6970746f6f6c732f636f6d706f7365726c6f636b)](https://packagist.org/packages/detain/iptools)

[![PHP 5.6](https://camo.githubusercontent.com/cc4f7ec05527d78bca32026f288b38eb12c861d1a7bdfb192d56a8742a05e2bb/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d352e362d3838393242462e737667)](http://php.net)[![PHP 7.0](https://camo.githubusercontent.com/de3caf721d9573c21b80a8ae09dbc79aebb5fef439bf68edc82b1e22aa98c6ec/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e302d3838393242462e737667)](http://php.net)[![PHP 7.1](https://camo.githubusercontent.com/48d6233e45d4c078e69111c68b9ecd4c6a5b60f7fa397cf0ea12386bf8215a07/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e312d3838393242462e737667)](http://php.net)[![PHP 7.2](https://camo.githubusercontent.com/cfd628dc653e38ca05e99444ae5189ffdbfc80c23e828332633fc9f866763d93/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e322d3838393242462e737667)](http://php.net)[![PHP 7.3](https://camo.githubusercontent.com/5a488385c057dc01c65df0848c4b4e99413d4bf6bf7a4776f5b054d37065e41c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e332d3838393242462e737667)](http://php.net)[![PHP 7.4](https://camo.githubusercontent.com/b955874d3e9269aa5e72f9d11ea2f4a1c9d907cd5d5a9c2361d646b962a91584/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e342d3838393242462e737667)](http://php.net)

Changes since fork
------------------

[](#changes-since-fork)

- 100% Code Coverage!
- Now Testing On PHP v5.6, v7.0, v7.1, v7.2, v7.3, and v7.4
- Added CodeClimate Coverage Reporting
- Added containsAny and containsAll to Range class for dealing with an array of IP/Range/Networks instead of a single one
- Added excludeArray to the Network class for dealing with an array of IP/Range/Networks to exclude instead of the single one with the existing exclude method

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

[](#installation)

Composer: Run in command line:

```
composer require detain/iptools

```

or put in composer.json:

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

Usage
-----

[](#usage)

This library breaks its handling down into *IP*, *Range*, and *Network* types and provdes a matching named class to make working with all things IP much simpler.

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

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

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

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

```

**Exclude Many Subnets from Network:**

```
$excluded = Network::parse('192.0.0.0/8')->excludeArray(['192.168.1.0/24', '192.168.2.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.3.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
```

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

**Check if IP is within any of the Ranges in an array:**

```
echo Range::parse('192.168.1.1-192.168.1.254')->containsAny([new IP('192.168.0.5'), new IP('192.168.1.5')]); // true
```

**Check if IP is within all of the Ranges in an array:**

```
echo Range::parse('192.168.1.1-192.168.1.254')->containsAll([new IP('192.168.0.5'), new IP('192.168.1.5')]); // false
echo Range::parse('192.168.1.1-192.168.1.254')->containsAll([new IP('192.168.1.6'), new IP('192.168.1.5')]); // 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
```

License
=======

[](#license)

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

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity64

Established project with proven stability

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

Recently: every ~144 days

Total

9

Last Release

2267d ago

Major Versions

v0.9.0 → v1.0.02015-12-11

v1.1.0 → v2.x-dev2016-08-25

v1.1.1 → v3.0.02020-04-17

PHP version history (3 changes)v0.9.0PHP &gt;=5.3.0

v1.0.0PHP &gt;=5.4.0

v3.0.1PHP &gt;=5.6.0

### Community

Maintainers

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

---

Top Contributors

[![S1lentium](https://avatars.githubusercontent.com/u/2018277?v=4)](https://github.com/S1lentium "S1lentium (61 commits)")[![detain](https://avatars.githubusercontent.com/u/1364504?v=4)](https://github.com/detain "detain (48 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

cidripip-addressip-geolocationip-lookupip-rangeip-toolsip-utilsip-validationip4ip6ipv4ipv6networknetworkingphpphp-libraryrangesubnetsubnet-calculatoripv6IPnetworkipv4cidrsubnetIP-Tools

###  Code Quality

TestsPHPUnit

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/detain-iptools/health.svg)](https://phpackages.com/packages/detain-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.3M68](/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)
