decodelabs/compass

解析、检查和测试IP地址

v0.2.0 2024-08-21 23:18 UTC

This package is auto-updated.

Last update: 2024-09-04 21:01:35 UTC


README

PHP from Packagist Latest Version Total Downloads GitHub Workflow Status PHPStan License

解析、检查和测试IP地址

Compass提供了对IPv4和IPv6 IP地址的精确反汇编以及检查和比较它们的方法。

DecodeLabs博客上获取新闻和更新。

安装

通过Composer安装

composer require decodelabs/compass

用法

解析IP字符串、整数和二进制并检查它们

use DecodeLabs\Compass\Ip;

$ip = Ip::parse('127.0.0.1');

if($ip->isV4()) {
    // Do something
}

if($ip->isLoopback()) {
    // Do something else
}

$v6Hybrid = $ip->toV6(); // ::ffff:127.0.0.1

$v6 = Ip::parse('fe80:0:0:0:202:b3ff:fe1e:8329');

if($ip->isV6()) {
    // The future
}

检查IP是否在某个范围内

if($ip->isIn('127.0.0.0/8')) {} // CIDR
if($ip->isIn('127.0.0.0/255.0.0.0')) {} // Netmask
if($ip->isIn('127.0.0.4-127.0.0.10')) {} // Range
if($ip->isIn('127.0.0.4+6')) {} // Relative range
if($ip->isIn('127.0.0.*')) {} // Wildcards

if($v6->isIn('fe80:0:0:0:202:b3ff:fe1e:0/128')) {} // CIDR
if($v6->isIn('fe80:0:0:0:202:b3ff:fe1e:0-fe80:0:0:0:202:b3ff:fe1e:ffff')) {} // Range
if($v6->isIn('fe80:0:0:0:202:b3ff:fe1e:0+9999')) {} // Relative range
if($v6->isIn('fe80:0:0:0:202:b3ff:fe1e:*')) {} // Wildcards

许可

Compass遵循MIT许可证。有关完整的许可证文本,请参阅LICENSE