Getting list IPs from CIDR notation in PHP

前端 未结 13 1363
慢半拍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 06:02

    I come up with a better idea

    $ip_from= long2ip(ip2long($ip)& (-1<<(32-$net_mask)));
    $ip_to= long2ip(ip2long($ip)| (~(-1<<(32-$net_mask))));
    

    P.S:$ip is a ipv4 address like 60.12.34.5;$net_mask is a int net mask like 25;

    it's very fast because of the bit shift operation

提交回复
热议问题