Search by

jycr753 / ip-utils

jycr753

small library to deal with IPv4 & IPv6 addresses and subnets

Package info

github.com/coquer/ip-utils

pkg:composer/jycr753/ip-utils

Statistics

Installs: 699 355

Dependents: 2

Suggesters: 0

Stars: 1

Open Issues: 0

v3.1.0 2026-11-30 20:09 UTC

This package is auto-updated.

Last update: 2026-11-30 20:12:30 UTC


README

Small PHP library for IPv4 and IPv6 addresses: validation, normalisation, and matching against literals, wildcard patterns and CIDR subnets.

Requires PHP 8.2+.

composer require jycr753/ip-utils

Usage

use IpUtils\Factory;

$ip = Factory::getAddress('2001:db8::1');   // IPv4 or IPv6
$ip->getExpanded();                         // 2001:0db8:0000:...:0001
$ip->getCompact();                          // 2001:db8::1

$ip->matches(Factory::getExpression('2001:db8::/32'));    // subnet
$ip->matches(Factory::getExpression('2001:db8::1'));      // literal

Factory::getAddress('10.1.2.3')->matches(Factory::getExpression('10.*.*.3'));  // pattern

Factory::getAddress('192.168.1.1')->isPrivate();   // also isLoopback(), isMulticast(), isLinkLocal() (IPv4)

Notes:

  • * in a pattern matches one or more digits within a single block. IPv6 patterns must list all 8 blocks (no ::).
  • Matching an IPv4 address against an IPv6 subnet (or the other way round) throws LogicException. With a pattern it throws UnexpectedValueException.
  • Invalid expressions throw IpUtils\Exception\InvalidExpressionException.

Development

composer install
composer test   # PHPUnit
composer lint   # PHPCS (PSR-12)