chialab / ip
Minimal library to manage IP addresses, subnets, netmasks, etc.
Installs: 14 060
Dependents: 1
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 0
Open Issues: 4
Requires
- php: >= 7.4
Requires (Dev)
- cakephp/cakephp-codesniffer: ^4.5
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.7
- phpstan/phpstan-phpunit: ^1.1
- phpstan/phpstan-webmozart-assert: ^1.2
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-11-07 02:14:28 UTC
README
This library for PHP 7.4+ builds an abstraction over management of Internet Protocol versions, addresses and CIDR blocks.
Using this library makes it easy to check if an IP address belongs to a subnet or not.
Installation
Installing this library can be done via Composer:
$ composer require chialab/ip
Usage
use Chialab\Ip; $address = Ip\Address::parse('192.168.1.1'); var_dump($address->getProtocolVersion() === Ip\ProtocolVersion::ipv4()); // bool(true) var_dump($address->getProtocolVersion() === Ip\ProtocolVersion::ipv6()); // bool(false) $subnet = Ip\Subnet::parse('fec0::1/16'); var_dump((string)$subnet->getFirstAddress()); // string(6): "fec0::" var_dump((string)$subnet->getNetmask()); // string(6) "ffff::" var_dump($subnet->contains($address)); // bool(false) var_dump($subnet->contains(Ip\Address::parse('fec0:fe08:0123:4567:89ab:cdef:1234:5678'))); // bool(true) var_dump($subnet->hasSubnet(Ip\Subnet::parse('fec0:fe08::/32'))); // bool(true)