PHP code example of markrogoyski / ipv4-subnet-calculator

1. Go to this page and download the library: Download markrogoyski/ipv4-subnet-calculator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

markrogoyski / ipv4-subnet-calculator example snippets



reate 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

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

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

$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(); // true

// 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 edAddressesFor(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"