function ip6_mask2cidr($mask) {
$s = '';
if (substr($mask, -1) == ':') $mask .= '0';
if (substr($mask, 0, 1) == ':') $mask = '0' . $mask;
if (strpos($mask, '::') !== false)
$mask = str_replace('::', str_repeat(':0', 8 - substr_count($mask, ':')).':', $mask);
foreach(explode(':',$mask) as $oct) {
// The following two lines, perhaps, superfluous.
// I left them because of the paranoia :)
$oct = trim($oct);
if ($oct == '') $s .= '0000000000000000';
else $s .= str_pad(base_convert($oct, 16, 2), 16, '0', STR_PAD_LEFT);
}
return strlen($s) - strlen(rtrim($s, '0'));
}
echo ip6_mask2cidr('ffff:ffff:ffff:ffff::') . "\n"; // 64
demo
[从CIDR前缀PHP5计算IPv6的范围?](的可能的复制http://stackoverflow.com/questions/10085266/php5-calculate-ipv6-range-from-cidr-prefix ) –
@TomaszKowalczyk我不在这里计算范围。你所说的重复问题只有相反的解决方案。 –