note 63778 deleted from function.ip2long by cmb
| From: | cmb@php.net | Date: | Sun, 18 Aug 2019 08:43:39 +0000 |
| Subject: | note 63778 deleted from function.ip2long by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211942@lists.php.net to get a copy of this message | ||
Note Submitter: legetz81 (AT) yahoo (dot) com
----
Here is a script that will calculate host range and number of hosts with a given ip address CIDR
notation (modified code which was posted by phl AT cyways.com):
<?php
$ip_addr_cidr = "192.168.37.215/27";
$ip_arr = explode('/', $ip_addr_cidr);
$bin = '';
for($i=1;$i<=32;$i++) {
$bin .= $ip_arr[1] >= $i ? '1' : '0';
}
$ip_arr[1] = bindec($bin);
$ip = ip2long($ip_arr[0]);
$nm = ip2long($ip_arr[1]);
$nw = ($ip & $nm);
$bc = $nw | (~$nm);
echo "Number of Hosts: " . ($bc - $nw - 1) . "\n";
echo "Host Range: " . long2ip($nw + 1) . " -> " . long2ip($bc - 1) .
"\n";
?>
This will produce:
Number of Hosts: 30
Host Range: 192.168.37.193 -> 192.168.37.222