markrogoyski/ipv4-subnet-calculator
Composer 安装命令:
composer require markrogoyski/ipv4-subnet-calculator
包简介
Network calculator for subnet mask and other classless (CIDR) network information.
README 文档
README
IPv4 Subnet Calculator (PHP)
Comprehensive PHP library for IPv4 subnet calculations, network planning, and CIDR operations.
Quick Start
<?php require_once(__DIR__ . '/vendor/autoload.php'); // Create from CIDR notation $subnet = IPv4\Subnet::fromCidr('192.168.1.0/24'); // Get network information echo $subnet->networkAddress(); // 192.168.1.0 echo $subnet->broadcastAddress(); // 192.168.1.255 echo $subnet->hostCount(); // 254 // Check if an IP is in the subnet $subnet->containsIP('192.168.1.100'); // true
Features
Core Capabilities
- Network Calculations - Subnet masks, wildcard masks, network/host portions, broadcast addresses
- IP Address Operations - Range detection, type classification (private, public, loopback, multicast, etc.)
- Network Analysis - Overlap detection, containment checking, conflict validation
- CIDR Operations - Aggregation, supernetting, subnet splitting
- Capacity Planning - Utilization analysis, optimal subnet sizing, waste calculation
- Advanced IPAM - Subnet exclusion, address space carving, sequential allocation
Output Formats
- Multiple formats: dotted decimal, hex, binary, integer, quads array
- Reports: JSON, associative arrays, formatted text, printed output
- Reverse DNS: IPv4 ARPA domain generation
Use Cases
- Network planning and IP address management (IPAM)
- Firewall rule validation and conflict detection
- BGP route summarization and optimization
- DHCP scope configuration
- DNS reverse zone setup
- Network automation and infrastructure-as-code
Installation
Using Composer (Command Line)
composer require markrogoyski/ipv4-subnet-calculator:5.*
Or Add to composer.json
{
"require": {
"markrogoyski/ipv4-subnet-calculator": "5.*"
}
}
Then run:
composer install
Requirements
- PHP 8.1+
- 64-bit architecture
- (For 32-bit architecture, use v4.4)
- Upgrading from v4? See the Migration Guide
Usage
Creating Subnets
// From CIDR notation (simple) $subnet = IPv4\Subnet::fromCidr('192.168.1.0/24'); $subnet = new IPv4\Subnet('192.168.1.0', 24); // From subnet mask $subnet = IPv4\SubnetParser::fromMask('192.168.1.0', '255.255.255.0'); // From IP range $subnet = IPv4\SubnetParser::fromRange('192.168.1.0', '192.168.1.255'); // From host count requirement $subnet = IPv4\SubnetParser::fromHostCount('192.168.1.0', 100);
Basic Network Information
$subnet = IPv4\Subnet::fromCidr('192.168.112.203/23'); // Network properties $subnet->networkAddress(); // 192.168.112.0 (returns IPAddress object) $subnet->broadcastAddress(); // 192.168.113.255 (returns IPAddress object) $subnet->hostCount(); // 510 // Subnet mask and wildcard (return value objects) $subnet->mask(); // 255.255.254.0 (SubnetMask object) $subnet->wildcardMask(); // 0.0.1.255 (WildcardMask object) // Address ranges (return IPRange objects) $subnet->addressRange(); // 192.168.112.0 - 192.168.113.255 (IPRange) $subnet->minHost(); // 192.168.112.1 (IPAddress) $subnet->maxHost(); // 192.168.113.254 (IPAddress)
Network Overlap & Conflict Detection
$subnet1 = IPv4\Subnet::fromCidr('192.168.1.0/24'); $subnet2 = IPv4\Subnet::fromCidr('192.168.1.128/25'); // Check for overlaps $subnet1->overlaps($subnet2); // true // Check containment $subnet1->contains($subnet2); // true $subnet2->isContainedIn($subnet1); // true // Check if IP is in subnet $subnet1->containsIP('192.168.1.100'); // true
IP Address Type Classification
$subnet = IPv4\Subnet::fromCidr('192.168.1.1/32'); $subnet->isPrivate(); // true (RFC 1918) $subnet->isPublic(); // false // Or check the IP address directly $ip = $subnet->ipAddress(); $ip->isPrivate(); // true $ip->addressType(); // AddressType::Private $loopback = IPv4\Subnet::fromCidr('127.0.0.1/32'); $loopback->isLoopback(); // true
Supported types: private, public, loopback, link-local, multicast, carrier-grade-nat, documentation, benchmarking, reserved, and more.
CIDR Aggregation & Route Summarization
// Aggregate multiple subnets into larger blocks $subnets = [ IPv4\Subnet::fromCidr('192.168.0.0/24'), IPv4\Subnet::fromCidr('192.168.1.0/24'), ]; $aggregated = IPv4\Subnets::aggregate($subnets); // Returns: [192.168.0.0/23] (array of Subnet objects) // Summarize to single supernet $summary = IPv4\Subnets::summarize($subnets); // Returns: 192.168.0.0/23 (Subnet object)
Subnet Exclusion (IPAM)
// Allocate a /24 but reserve the first /26 for infrastructure $allocated = IPv4\Subnet::fromCidr('192.168.1.0/24'); $reserved = IPv4\Subnet::fromCidr('192.168.1.0/26'); $available = $allocated->exclude($reserved); // Returns: [192.168.1.64/26, 192.168.1.128/25] (array of Subnet objects) // Exclude multiple subnets $available = $allocated->excludeAll([$reserved1, $reserved2, $reserved3]);
Utilization & Capacity Planning
$subnet = IPv4\Subnet::fromCidr('192.168.1.0/24'); // Analyze utilization for host requirements $utilization = $subnet->utilizationFor(100); // 39.37% $wasted = $subnet->wastedAddressesFor(100); // 154 // Find optimal subnet size $optimalPrefix = IPv4\SubnetParser::optimalPrefixForHosts(100); // Returns: 25 (a /25 provides 126 usable hosts)
Subnet Operations
$subnet = IPv4\Subnet::fromCidr('192.168.112.0/23'); // Split into smaller subnets $smaller = $subnet->split(25); // Split /23 into /25 subnets (array of Subnet objects) // Navigate to adjacent subnets $next = $subnet->next(); // 192.168.114.0/23 (Subnet object) $prev = $subnet->previous(); // 192.168.110.0/23 (Subnet object)
Generate Reports
$subnet = IPv4\Subnet::fromCidr('192.168.112.203/23'); // Get as array $array = $subnet->toArray(); // Get as JSON $json = $subnet->toJson(); // Get as formatted string report $report = $subnet->toPrintable(); // String representation (CIDR notation) echo $subnet; // "192.168.112.203/23"
Example Report Output:
192.168.112.203/23 Quads Hex Binary Integer
------------------ --------------- -------- -------------------------------- ----------
IP Address: 192.168.112.203 C0A870CB 11000000101010000111000011001011 3232264395
Subnet Mask: 255.255.254.0 FFFFFE00 11111111111111111111111000000000 4294966784
Network Portion: 192.168.112.0 C0A87000 11000000101010000111000000000000 3232264192
Address Type: private
Number of IP Addresses: 512
Number of Addressable Hosts: 510
IP Address Range: 192.168.112.0 - 192.168.113.255
Complete Report Documentation →
Documentation
Getting Started
- Installation & Setup - Get up and running quickly
- Creating Subnet Calculators - All factory methods
- Basic Usage Examples - Common operations
Core Features
- Network Component Access - IP addresses, masks, network/host portions
- Network Overlap & Containment - Conflict detection
- IP Address Type Detection - Classify addresses (private, public, etc.)
- Subnet Operations - Split, navigate, reverse DNS
- Adjacent Subnet Navigation - Sequential subnet allocation
Advanced Features
- Subnet Exclusion - IPAM and address carving
- CIDR Aggregation - Route summarization
- Utilization Statistics - Capacity planning
- Network Classes - Legacy classful networking
Reference & Examples
- API Reference - Complete method documentation
- Reports - All output formats and examples
- Real-World Examples - IPAM, firewall rules, BGP, DHCP, DNS, automation
Upgrading
- Migration Guide: v4 to v5 - Upgrade from v4.x with step-by-step instructions
Quick Links
- Documentation Index - Complete navigation and learning paths
- Quick Reference - Common operations at a glance
Unit Tests
cd tests
phpunit
Standards
IPv4 Subnet Calculator (PHP) conforms to the following standards:
- PSR-1 - Basic coding standard (http://www.php-fig.org/psr/psr-1/)
- PSR-4 - Autoloader (http://www.php-fig.org/psr/psr-4/)
- PSR-12 - Extended coding style guide (http://www.php-fig.org/psr/psr-12/)
License
IPv4 Subnet Calculator (PHP) is licensed under the MIT License.
markrogoyski/ipv4-subnet-calculator 适用场景与选型建议
markrogoyski/ipv4-subnet-calculator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 855.71k 次下载、GitHub Stars 达 175, 最近一次更新时间为 2016 年 04 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「network」 「address」 「IP」 「ipv4」 「subnet」 「calculator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 markrogoyski/ipv4-subnet-calculator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 markrogoyski/ipv4-subnet-calculator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 markrogoyski/ipv4-subnet-calculator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Maps in minutes. Powered by the Google Maps API.
Redirects TYPO3 visitors automatic or with a suggestlink to another language and/or root page.
A stand-alone class implementation of the IPv4+IPv6 IP+CIDR aggregator from CIDRAM.
Module adding custom shipping attribute for what3words address
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
PHP Library for manipulating network addresses (IPv4 and IPv6)
统计信息
- 总下载量: 855.71k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 178
- 点击次数: 30
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-04-13
