PHP library for IPv4 subnet calculations, CIDR operations, and network planning.
PHP library for IPv4 subnet calculations, CIDR operations, and network planning.
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'); // truecomposer require markrogoyski/ipv4-subnet-calculator:5.*{
"require": {
"markrogoyski/ipv4-subnet-calculator": "5.*"
}
}Then run:
composer install// 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);…$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$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(); // trueSupported types: private, public, loopback, link-local, multicast, carrier-grade-nat, documentation, benchmarking, reserved, and more.
// 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)// 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 = 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 = 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 = 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 →
cd tests
phpunitIPv4 Subnet Calculator (PHP) conforms to the following standards:
IPv4 Subnet Calculator (PHP) is licensed under the MIT License.
No open issues yet, or sync has not completed.