$ip = ‘192.168.6.1‘; $mask = ‘255.255.2.0‘; $sub_net = array();//子网 $ip_explode = explode(‘.‘, $ip); $mask_explode = explode(‘.‘, $mask); foreach ($ip_explode as $key => $ip_num) { //10进制转2进制 $a = decbin($ip_num); $b = decbin($mask_explode[$key]); //补0补够8位 $a = sprintf("%08d", $a); $b = sprintf("%04d", $b);//前补0 $b = sprintf("%0.4f", $b);//后取多少位 echo $a."==".$b."\n"; $c = $a & $b; echo $c."\n"; $d = bindec($c); echo $d."\n"; $sub_net[] = $d; } $sub_net = implode(‘.‘, $sub_net); echo $sub_net;
时间: 2024-10-23 04:10:30