Pular para o conteúdo
← Voltar para projetos

Ipv4 Subnet Calculator PHP

Network calculator for subnet mask and other classless (CIDR) network information.

IPV4 Subnet Calculator Logo

#IPv4 Subnet Calculator (PHP)

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

Coverage Status License Latest Stable Version Downloads

#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

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)

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+
    • (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

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

$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 Component Access →

#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

Overlap Detection Guide →

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

All IP Address Types →

#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)

CIDR Aggregation Guide →

#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]);

Subnet Exclusion Guide →

#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)

Utilization Analysis Guide →

#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)

Subnet Operations Guide →

#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

#Core Features

#Advanced Features

#Reference & Examples

#Upgrading

#Quick Links

#Unit Tests

cd tests
phpunit

Coverage Status

#Standards

IPv4 Subnet Calculator (PHP) conforms to the following standards:

#License

IPv4 Subnet Calculator (PHP) is licensed under the MIT License.

Nova versão disponível.