百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
I

ipv4-subnet-calculator-php

> DevOps
开源

用于进行 IPv4 子网计算、CIDR 操作和网络规划的 PHP 库。

175 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

用于进行 IPv4 子网计算、CIDR 操作和网络规划的 PHP 库。

IPv4 Subnet Calculator (PHP)

Comprehensive PHP library for IPv4 subnet calculations, network planning, and CIDR operations. Quick Start

php
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

View Complete Documentation →

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

Complete Feature List →

Installation

Using Composer (Command Line)

bash
composer require markrogoyski/ipv4-subnet-calculator:5.*

Or Add to composer.json

json
{
  "require": {
    "markrogoyski/ipv4-subnet-calculator": "5.*"
  }
}

Then run:

bash
composer install

Requirements

  • PHP 8.1+
    • (For PHP 7.2–8.0, use v4.4)
    • (For PHP 5.5–7.1, use v3.1)
  • 64-bit architecture
    • (For 32-bit architecture, use v4.4)
  • Upgrading from v4? See the Migration Guide

Detailed Installation Guide →

Usage

Creating Subnets

php
// 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);

All Creation Methods →

Basic Network Information

…

Network Component Access →

Network Overlap & Conflict Detection

php
$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

Overlap Detection Guide →

IP Address Type Classification

php
$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.

All IP Address Types →

CIDR Aggregation & Route Summarization

php
// 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)

CIDR Aggregation Guide →

Subnet Exclusion (IPAM)

php
// 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]);

Subnet Exclusion Guide →

Utilization & Capacity Planning

php
$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)

Utilization Analysis Guide →

Subnet Operations

php
$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)

Subnet Operations Guide →

Generate Reports

php
$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:

…

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

bash
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.

Issues· 1 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPcidrcidr-subnetipip-address

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类DevOps
定价开源

> 相关工具

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理