United Statesmarkrogoyski/ipv4-subnet-calculator-php
PHP library for IPv4 subnet calculations, CIDR operations, and network planning.

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.
Similar Projects
Discover other similar projects and AI models.
a list of domains and ips that stay live in russia when the mobile internet gets restricted
💻 a library for parsing and manipulating IPv4 and IPv6 addresses in JavaScript
Go library for handling IP addresses and subnets, both IPv4 and IPv6
Ip2region is an offline IP-to-Region localization library and IP data management framework with both IPv4 and IPv6 supports, 10-microsecond level query efficiency, xdb search client for many programming languages
The best IP Toolbox. Check your IP address & geolocation, test IP for WebRTC and DNS IP leaks, run an IP quality check, browser fingerprint check, website availability check, network speed test, global latency test, MTR test, Whois search, and more.