PHPackages                             pjialin/ip-addr - 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. pjialin/ip-addr

ActiveLibrary

pjialin/ip-addr
===============

Manages IPv4 and IPv6 addresses and subnets

0.6.4(7y ago)014BSD-3-ClausePHPPHP &gt;=5.4.0

Since Nov 3Pushed 7y ago1 watchersCompare

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

READMEChangelog (1)Dependencies (1)Versions (11)Used By (0)

IP-Addr library manages IPv4 and IPv6 addresses and subnets
===========================================================

[](#ip-addr-library-manages-ipv4-and-ipv6-addresses-and-subnets)

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

[](#installation)

Install with composer:

```
composer require bis-gmbh/ip-addr

```

### Installation requirements

[](#installation-requirements)

- PHP version 5.4 or above
- PHP GMP extension for IPv6 arbitrary arithmetic

Abstract usage example
----------------------

[](#abstract-usage-example)

```
use \BIS\IPAddr\Utils as IP;
use \BIS\IPAddr\HostIterator;
use \BIS\IPAddr\SubnetIterator;

$providerSubnet = IP::make('10.0/8');

$userSubnet = IP::make('10.100.0.2/30');

if ($providerSubnet->contains($userSubnet)) {
    printf("User network: %s\n", $userSubnet->network()->addr());
    printf("User broadcast: %s\n", $userSubnet->broadcast()->addr());
    printf("User addrs:\n");
    foreach ($userSubnet as $index => $ip) {
        printf("%d: %s\n", $index, $ip->addr());
    }
    $userHosts = new HostIterator($userSubnet);
    printf("User hosts:\n");
    foreach ($userHosts as $index => $ip) {
        printf("%d: %s\n", $index, $ip->addr());
    }
}

printf("Provider subnets:\n");
$providerSubnets = new SubnetIterator($providerSubnet, 10);
foreach ($providerSubnets as $index => $subnet) {
    printf("%d: %s\n", $index, $subnet->cidr());
}
```

Will output:

```
User network: 10.100.0.0
User broadcast: 10.100.0.3
User addrs:
0: 10.100.0.0
1: 10.100.0.1
2: 10.100.0.2
3: 10.100.0.3
User hosts:
0: 10.100.0.1
1: 10.100.0.2
Provider subnets:
0: 10.0.0.0/10
1: 10.64.0.0/10
2: 10.128.0.0/10
3: 10.192.0.0/10

```

Used by
-------

[](#used-by)

- [2ip.io IP calculator](https://2ip.io/ip-calc/)

API
---

[](#api)

### Constructor

[](#constructor)

#### 🔹 v4::create | v6::create

[](#small_blue_diamond-v4create--v6create)

###### Description

[](#description)

```
public static function create ( $anyFormat [, string $maskString = null ] ) : Address;

```

###### Parameters

[](#parameters)

- *anyFormat*
    - integer, e.g. `123`, `0xABCDEF00`, `075227`, `0b0101010`
    - numeric string, e.g. `'123'`, `'0xABCDEF00'`, `'075227'`, `'0b0101010'`
    - textual format:
        - v4, `'192.168.10.1'`, `'172.16.3'`, `'10.0'`
        - v6, `'a:b:c:d::'`, `'::'`, `'::345d:10.40.60.1'`
    - CIDR format:
        - v4, `'10.0.0.0/8'`, `'192.168/16'`
        - v6, `'2000:b:c:d::/64'`, `'::1/128'`
    - range format `' - '`, where `` and `` - addresses in textual format with the same ip version
        - v4 `'10.0.0.0 - 10.0.0.255'`
        - v6 `'2000:b:c:d::5d - 2000:b:c:d::ff'`
- *maskString* network mask in textual format, allow if *anyFormat* parameter also in textual format
    - v4 `'255.255.255.0'`, `'255.0'`
    - v6 `'ffff:ffff:ffff:ffff::'`

###### Return values

[](#return-values)

Returns a `Address` object on success

###### Examples

[](#examples)

```
$v4instance = v4::create('127.0.0.1');
$v4subnet = v4::create('192.168.0.1', '255.255.255.0');
$v6instance = v6::create('::1');
```

#### 🔹 new v4 | new v6

[](#small_blue_diamond-new-v4--new-v6)

###### Description

[](#description-1)

Creates a `Address` instances by **new** operator with the same parameters as the method `create`.

###### Examples

[](#examples-1)

```
$v4instance = new v4('127.0.0.1');
$v6instance = new v6('::1');
```

### Class

[](#class)

#### Methods

[](#methods)

##### 🔹 v4::isNumeric | v6::isNumeric

[](#small_blue_diamond-v4isnumeric--v6isnumeric)

###### Description

[](#description-2)

```
public static function isNumeric ( $value ) : bool;

```

Checks if the parameter *value* present in numeric format.

###### Parameters

[](#parameters-1)

- *value* - verified value

###### Return values

[](#return-values-1)

Returns **TRUE** if the *value* in numeric format, and **FALSE** if not.

###### Examples

[](#examples-2)

```
var_dump(v4::isNumeric(0xFF000000));
var_dump(v4::isNumeric('0xFF000000'));
var_dump(v4::isNumeric('abcdef'));
var_dump(v4::isNumeric(true));
```

```
bool(true)
bool(true)
bool(false)
bool(false)

```

##### 🔹 v4::isTextual | v6::isTextual

[](#small_blue_diamond-v4istextual--v6istextual)

###### Description

[](#description-3)

```
public static function isTextual ( $value ) : bool;

```

Checks if the parameter *value* present in textual format.

###### Parameters

[](#parameters-2)

- *value* - verified value

###### Return values

[](#return-values-2)

Returns **TRUE** if the *value* in textual format, and **FALSE** if not.

```
var_dump(v6::isTextual('1111:2222::5555:6666:7777:8888'));
var_dump(v6::isTextual('::ffff:2.3.4.0'));
var_dump(v6::isTextual('::/'));
var_dump(v6::isTextual('::ffff:2.3.4'));
```

```
bool(true)
bool(true)
bool(false)
bool(false)

```

##### 🔹 v4::isCIDR | v6::isCIDR

[](#small_blue_diamond-v4iscidr--v6iscidr)

###### Description

[](#description-4)

```
public static function isCIDR ( $value ) : bool;

```

Checks if the parameter *value* present in CIDR format.

###### Parameters

[](#parameters-3)

- *value* - verified value

###### Return values

[](#return-values-3)

Returns **TRUE** if the *value* in CIDR format, and **FALSE** if not.

```
var_dump(v6::isCIDR('2000:2222::5555:6666:7777:8888/64'));
var_dump(v6::isCIDR('::/128'));
var_dump(v6::isCIDR('::'));
var_dump(v6::isCIDR('::/129'));
```

```
bool(true)
bool(true)
bool(false)
bool(false)

```

##### 🔹 v4::isRange | v6::isRange

[](#small_blue_diamond-v4isrange--v6isrange)

###### Description

[](#description-5)

```
public static function isRange ( $value ) : bool;

```

Checks if the parameter *value* present in range format. Allows any order of addresses - direct or reverse, e.g. `'10.0.0.255 - 10.0.0.0'` is allowed range.

###### Parameters

[](#parameters-4)

- *value* - verified value

###### Return values

[](#return-values-4)

Returns **TRUE** if the *value* in range format, and **FALSE** if not.

###### Examples

[](#examples-3)

```
var_dump(v4::isRange('10.0 - 10.10'));
var_dump(v4::isRange('192.168.0.1-   192.168.255.255'));
var_dump(v4::isRange('127.0.0.0-'));
var_dump(v4::isRange('127.0.0.0-::1'));
```

```
bool(true)
bool(true)
bool(false)
bool(false)

```

### Instance

[](#instance)

#### Properties

[](#properties)

##### 🔸 v4::$privateNetworks

[](#small_orange_diamond-v4privatenetworks)

###### Description

[](#description-6)

Array of private v4 networks as described in [rfc1918](https://tools.ietf.org/html/rfc1918#section-3).

##### 🔸 v4::$multicastNetworks

[](#small_orange_diamond-v4multicastnetworks)

###### Description

[](#description-7)

Array of multicast v4 networks as described in [rfc3171](https://tools.ietf.org/html/rfc3171).

##### 🔸 v4::$reservedNetworks

[](#small_orange_diamond-v4reservednetworks)

###### Description

[](#description-8)

Array of reserved v4 networks as described in [RFC 1112, Section 4](https://tools.ietf.org/html/rfc1112#section-4).

##### 🔸 v4::$networkTypes

[](#small_orange_diamond-v4networktypes)

###### Description

[](#description-9)

Array of associative arrays representing special-purpose v4 addresses with their descriptions, [rfc5735](https://tools.ietf.org/html/rfc5735#section-4).

##### 🔸 v6::$addressTypes

[](#small_orange_diamond-v6addresstypes)

###### Description

[](#description-10)

Array of associative arrays representing v6 address types with their descriptions, [rfc4291](https://tools.ietf.org/html/rfc4291).

#### Methods

[](#methods-1)

##### 🔹 v4::version | v6::version

[](#small_blue_diamond-v4version--v6version)

###### Description

[](#description-11)

```
public function version ( void ) : int;

```

###### Return values

[](#return-values-5)

Returns the version number of the address of the current object.

###### Examples

[](#examples-4)

```
var_dump(v4::create('10.0.0.0')->version());
var_dump(v6::create('::1')->version());
```

```
int(4)
int(6)

```

##### 🔹 v4::assign | v6::assign

[](#small_blue_diamond-v4assign--v6assign)

###### Description

[](#description-12)

```
public static function assign ( $anyFormat [, string $maskString = null ] ) : Address;

```

Assigns new address and mask values for the current object.

###### Parameters

[](#parameters-5)

See [create](#small_blue_diamond-v4create--v6create) method.

###### Examples

[](#examples-5)

```
$ip = v4::create('127.0.0.1');
var_dump($ip->addr());
$ip->assign('192.168.0.1');
var_dump($ip->addr());
```

```
string(9) "127.0.0.1"
string(11) "192.168.0.1"

```

##### 🔹 v4::binary | v6::binary

[](#small_blue_diamond-v4binary--v6binary)

TODO

##### 🔹 v4::decimal | v6::decimal

[](#small_blue_diamond-v4decimal--v6decimal)

TODO

##### 🔹 v4::hexadecimal | v6::hexadecimal

[](#small_blue_diamond-v4hexadecimal--v6hexadecimal)

TODO

##### 🔹 v4::netmask | v6::netmask

[](#small_blue_diamond-v4netmask--v6netmask)

TODO

##### 🔹 v4::prefixLength | v6::prefixLength

[](#small_blue_diamond-v4prefixlength--v6prefixlength)

TODO

##### 🔹 v4::first | v6::first

[](#small_blue_diamond-v4first--v6first)

TODO

##### 🔹 v4::last | v6::last

[](#small_blue_diamond-v4last--v6last)

TODO

##### 🔹 v4::numAddrs | v6::numAddrs

[](#small_blue_diamond-v4numaddrs--v6numaddrs)

TODO

##### 🔹 v4::numHosts | v6::numHosts

[](#small_blue_diamond-v4numhosts--v6numhosts)

TODO

##### 🔹 v4::hostBits | v6::hostBits

[](#small_blue_diamond-v4hostbits--v6hostbits)

TODO

##### 🔹 v4::within | v6::within

[](#small_blue_diamond-v4within--v6within)

TODO

##### 🔹 v4::contains | v6::contains

[](#small_blue_diamond-v4contains--v6contains)

TODO

##### 🔹 v4::addr | v6::addr

[](#small_blue_diamond-v4addr--v6addr)

TODO

##### 🔹 v4::mask | v6::mask

[](#small_blue_diamond-v4mask--v6mask)

TODO

##### 🔹 v4::cidr | v6::cidr

[](#small_blue_diamond-v4cidr--v6cidr)

TODO

##### 🔹 v4::range | v6::range

[](#small_blue_diamond-v4range--v6range)

TODO

##### 🔹 v4::reverse | v6::reverse

[](#small_blue_diamond-v4reverse--v6reverse)

TODO

##### 🔹 v4::reverseMask | v6::reverseMask

[](#small_blue_diamond-v4reversemask--v6reversemask)

TODO

##### 🔹 v4::netType | v6::netType

[](#small_blue_diamond-v4nettype--v6nettype)

TODO

##### 🔹 v4::network

[](#small_blue_diamond-v4network)

TODO

##### 🔹 v4::broadcast

[](#small_blue_diamond-v4broadcast)

TODO

##### 🔹 v4::netClass

[](#small_blue_diamond-v4netclass)

TODO

##### 🔹 v6::full

[](#small_blue_diamond-v6full)

TODO

##### 🔹 v6::full4

[](#small_blue_diamond-v6full4)

TODO

##### 🔹 v6::fullMask

[](#small_blue_diamond-v6fullmask)

TODO

##### 🔹 v6::compressed

[](#small_blue_diamond-v6compressed)

TODO

##### 🔹 v6::compressed4

[](#small_blue_diamond-v6compressed4)

TODO

### Array access

[](#array-access)

TODO

### Iterators

[](#iterators)

#### Address iteration

[](#address-iteration)

TODO

#### Host iteration

[](#host-iteration)

TODO

#### Subnet iteration

[](#subnet-iteration)

TODO

### Exceptions

[](#exceptions)

Static methods and methods of objects can throw exceptions of the following types:

- `\InvalidArgumentException` when calling a method with incorrect arguments;
- `\DomainException` when trying to use language constructs that do not apply to address objects, for example, overwriting an element when accessing an array by index key;
- `\RuntimeException` when the library classes can not work in the current environment, for example, PHP GMP extension not installed.

```
try {
    $ip = v6::create('invalid addr');
} catch (\InvalidArgumentException $e) {
    echo $e->getMessage() . PHP_EOL;
}

try {
    $ip = v6::create('2002::fdce/64');
    echo $ip[12]->addr() . PHP_EOL;
    $ip[12] = 0;
} catch (\DomainException $e) {
    echo $e->getMessage() . PHP_EOL;
}
```

```
Wrong arguments
2002::c
Read-only access

```

### Utils Class

[](#utils-class)

#### Methods

[](#methods-2)

##### 🔹 make

[](#small_blue_diamond-make)

###### Description

[](#description-13)

```
public static function make ( $anyFormat [, string $maskString = null ] ) : Address

```

Trying to create an object of any of the versions based on the provided arguments

###### Parameters

[](#parameters-6)

See [create](#small_blue_diamond-v4create--v6create) method.

###### Return values

[](#return-values-6)

Returns a `Address` object on success

###### Examples

[](#examples-6)

```
use \BIS\IPAddr\Utils as IP;

var_dump(IP::make('127.0.0.1')->version());
var_dump(IP::make('::1')->version());
```

```
int(4)
int(6)

```

##### 🔹 info

[](#small_blue_diamond-info)

###### Description

[](#description-14)

```
public static function info ( Address $addr ) : array

```

###### Parameters

[](#parameters-7)

*addr* - `v4` or `v6` object

###### Return values

[](#return-values-7)

Returns the array with summary information about given address.

###### Examples

[](#examples-7)

```
use \BIS\IPAddr\Utils as IP;

$ip = IP::make('127.0.0.1/8');
echo json_encode(IP::info($ip), JSON_PRETTY_PRINT) . PHP_EOL;
```

```
{
    "ver": 4,
    "host": {
        "addr": "127.0.0.1",
        "bin": "0b01111111000000000000000000000001",
        "dec": 2130706433,
        "hex": "0x7f000001",
        "raddr": "1.0.0.127.in-addr.arpa.",
        "type": "Loopback"
    },
    "net": {
        "cidr": "127.0.0.1\/8",
        "range": "127.0.0.0 - 127.255.255.255",
        "masklen": 8,
        "hostbits": 24,
        "mask": "255.0.0.0",
        "rmask": "0.0.0.255",
        "addrs": 16777216,
        "hosts": 16777214,
        "network": "127.0.0.0",
        "broadcast": "127.255.255.255",
        "class": "A"
    }
}

```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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

Recently: every ~76 days

Total

10

Last Release

2802d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0b17be99cb253e9b9e5affcdb1e8f5492456c172a211af5f6449b60ae821edf2?d=identicon)[pjialin](/maintainers/pjialin)

---

Top Contributors

[![nezhelskoy](https://avatars.githubusercontent.com/u/2945846?v=4)](https://github.com/nezhelskoy "nezhelskoy (37 commits)")

---

Tags

phpipv6libraryIPipv4cidrRFC4291rfc791rfc4632

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/pjialin-ip-addr/health.svg)

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

###  Alternatives

[zoujingli/ip2region

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

1.4k437.1k56](/packages/zoujingli-ip2region)[s1lentium/iptools

PHP Library for manipulating network addresses (IPv4 and IPv6)

2446.2M24](/packages/s1lentium-iptools)[darsyn/ip

An immutable IP Address value object that provides several different notations, including helper functions.

2572.0M20](/packages/darsyn-ip)[markrogoyski/ipv4-subnet-calculator

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

177813.7k6](/packages/markrogoyski-ipv4-subnet-calculator)[rlanvin/php-ip

IPv4/IPv6 manipulation library for PHP

180738.8k11](/packages/rlanvin-php-ip)[tholu/php-cidr-match

CIDRmatch is a library to match an IP to an IP range in CIDR notation (IPv4 and IPv6).

37211.1k](/packages/tholu-php-cidr-match)

PHPackages © 2026

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