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我不在這裏計算範圍。你所說的重複問題只有相反的解決方案。 –