Back to Trending
RepositoryPHPUSUnited States
markrogoyski

markrogoyski/ipv4-subnet-calculator-php

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

175stars
41forks
1issues
2views

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

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.

Similar Projects

Discover other similar projects and AI models.

Repository
hxehex/russia-mobile-internet-whitelist

a list of domains and ips that stay live in russia when the mobile internet gets restricted

3.8k
Jul 24, 2026
RepositoryTypeScript
beaugunderson/ip-address

💻 a library for parsing and manipulating IPv4 and IPv6 addresses in JavaScript

636
Nov 28, 2026
RepositoryGo
seancfoley/ipaddress-go

Go library for handling IP addresses and subnets, both IPv4 and IPv6

118
Nov 5, 2026
RepositoryPHP
CIDRAM/CIDRAM

CIDRAM: Classless Inter-Domain Routing Access Manager.

112
Nov 23, 2026
RepositoryGo
lionsoul2014/ip2region

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

20.0k
Nov 24, 2026
RepositoryJavaScript
jason5ng32/MyIP

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.

12.5k
Nov 28, 2026
RepositoryJavaScript
metowolf/iplist

IP CIDRs List / IP 地址列表

1.8k
Nov 30, 2026
RepositoryHTML
itbdw/ip-database

免费IP离线数据库,支持IPV4+IPV6 ,国家、省、市、县、运营商

1.5k
Dec 1, 2023
RepositoryPython
SunsetMkt/anti-ip-attribution

针对部分网站显示IP归属地的流量分流规则

1.1k
Nov 23, 2026