1) return false; // Resolve "::" // "::" auflösen if ($ip_addr == '::') $ip_addr = '0:0:0:0:0:0:0:0'; $ip_addr = preg_replace('_^::_', '*:', $ip_addr); $ip_addr = preg_replace('_::$_', ':*', $ip_addr); $ip_addr = str_replace('::', ':*:', $ip_addr); $cnt = count(explode(':', $ip_addr)); if ($cnt <= 8) { if (strpos($ip_addr, ':*') !== false) $ip_addr = str_replace(':*', str_repeat(':0', 8 - ($cnt - 1)), $ip_addr); else if (strpos($ip_addr, '*:') !== false) $ip_addr = str_replace('*:', str_repeat('0:', 8 - ($cnt - 1)), $ip_addr); } // Split and check chunks // Blöcke zerlegen und prüfen $expanded = ''; $chunks = explode(':', $ip_addr); if (count($chunks) != 8) return false; foreach ($chunks as $chunk) { if (!preg_match('/^[0-9a-f]{1,4}$/', $chunk)) return false; $expanded .= (strlen($expanded) > 0 ? ':' : '') . str_pad($chunk, 4, '0', STR_PAD_LEFT); } return $expanded; } // Shortens an IPv6 address into its shortest-possible form. // // ip_addr = (string) IPv6 address to shorten. // // Returns (string) Shortened IPv6 address, or {{false}} for invalid input. // function ShortenIPv6($ip_addr) { // Normalise and check address // Adresse normalisieren und prüfen $ip_addr = ExpandIPv6($ip_addr); if ($ip_addr === false) return false; // Remove leading zeroes // Führende Nullen entfernen $ip_addr = preg_replace('_(?<=^|:)0+(?=[0-9a-f]+(:|$))_', '', $ip_addr); // Find largest zero chunk and replace it with "::" once // Längsten Null-Teil einmal mit "::" ersetzen $ip_addr = preg_replace('_^0:0:0:0:0:0:0:0$_', '::', $ip_addr, 1); if (strpos($ip_addr, '::') === false) $ip_addr = preg_replace('_(^|:)0:0:0:0:0:0:0(:|$)_', '::', $ip_addr, 1); if (strpos($ip_addr, '::') === false) $ip_addr = preg_replace('_(^|:)0:0:0:0:0:0(:|$)_', '::', $ip_addr, 1); if (strpos($ip_addr, '::') === false) $ip_addr = preg_replace('_(^|:)0:0:0:0:0(:|$)_', '::', $ip_addr, 1); if (strpos($ip_addr, '::') === false) $ip_addr = preg_replace('_(^|:)0:0:0:0(:|$)_', '::', $ip_addr, 1); if (strpos($ip_addr, '::') === false) $ip_addr = preg_replace('_(^|:)0:0:0(:|$)_', '::', $ip_addr, 1); if (strpos($ip_addr, '::') === false) $ip_addr = preg_replace('_(^|:)0:0(:|$)_', '::', $ip_addr, 1); if (strpos($ip_addr, '::') === false) $ip_addr = preg_replace('_(^|:)0(:|$)_', '::', $ip_addr, 1); return $ip_addr; } ?>