Getting list IPs from CIDR notation in PHP

前端 未结 13 1337
慢半拍i
慢半拍i 2020-12-08 05:38

Is there a way (or function/class) to get the list of IP addresses from a CIDR notation?

For example, I have 73.35.143.32/27 CIDR and want to get the list of all IP\

13条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 05:52

    Hej. I also needed this function and I edit 1 other to return me list of all IP addresses from range. This will return list of IP addresses from CIDR Notation. Enjoy it ;)

    = $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) . "
    "; echo "Host Range: " . long2ip($nw + 1) . " -> " . long2ip($bc - 1) . "
    ". "
    "; for($zm=1;($nw + $zm)<=($bc - 1);$zm++) { echo long2ip($nw + $zm). "
    "; } ?>

提交回复
热议问题