jycr753 / ip-utils
small library to deal with IPv4 & IPv6 addresses and subnets
v3.1.0
2026-11-30 20:09 UTC
Requires
- php: ^8.2
Requires (Dev)
- phpunit/phpunit: ^11.5
- squizlabs/php_codesniffer: ^4.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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 throwsUnexpectedValueException. - Invalid expressions throw
IpUtils\Exception\InvalidExpressionException.
Development
composer install composer test # PHPUnit composer lint # PHPCS (PSR-12)