Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chialab/php-ip
Minimal PHP library to manage IP addresses, subnets, netmasks, etc. without additional dependencies
https://github.com/chialab/php-ip
cidr-notation internet-protocol ip-address php php7 php8
Last synced: about 2 months ago
JSON representation
Minimal PHP library to manage IP addresses, subnets, netmasks, etc. without additional dependencies
- Host: GitHub
- URL: https://github.com/chialab/php-ip
- Owner: chialab
- License: mit
- Created: 2022-11-21T12:08:12.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-03-06T22:10:09.000Z (almost 2 years ago)
- Last Synced: 2024-10-11T09:44:32.249Z (3 months ago)
- Topics: cidr-notation, internet-protocol, ip-address, php, php7, php8
- Language: PHP
- Homepage:
- Size: 86.9 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
IP Address library for PHP
==========================[![Run tests](https://github.com/chialab/php-ip/actions/workflows/test.yml/badge.svg)](https://github.com/chialab/php-ip/actions/workflows/test.yml)
[![codecov](https://codecov.io/gh/chialab/php-ip/branch/main/graph/badge.svg?token=T3LTGXYGOJ)](https://codecov.io/gh/chialab/php-ip)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:
```console
$ composer require chialab/ip
```## Usage
```php
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)
```