SimplePie_Net_IPv6::Uncompress PHP Method

Uncompress() public method

RFC 2373 allows you to compress zeros in an address to '::'. This function expects an valid IPv6 address and expands the '::' to the required zeros. Example: FF01::101 -> FF01:0:0:0:0:0:0:101 ::1 -> 0:0:0:0:0:0:0:1
public Uncompress ( string $ip ) : string
$ip string a valid IPv6-address (hex format)
return string the uncompressed IPv6-address (hex format)
    function Uncompress($ip)
    {
        $uip = SimplePie_Net_IPv6::removeNetmaskSpec($ip);
        $c1 = -1;
        $c2 = -1;
        if (strpos($ip, '::') !== false) {
            list($ip1, $ip2) = explode('::', $ip);
            if ($ip1 === '') {
                $c1 = -1;
            } else {
                $pos = 0;
                if (($pos = substr_count($ip1, ':')) > 0) {
                    $c1 = $pos;
                } else {
                    $c1 = 0;
                }
            }
            if ($ip2 === '') {
                $c2 = -1;
            } else {
                $pos = 0;
                if (($pos = substr_count($ip2, ':')) > 0) {
                    $c2 = $pos;
                } else {
                    $c2 = 0;
                }
            }
            if (strstr($ip2, '.')) {
                $c2++;
            }
            // ::
            if ($c1 === -1 && $c2 === -1) {
                $uip = '0:0:0:0:0:0:0:0';
            } else {
                if ($c1 === -1) {
                    $fill = str_repeat('0:', 7 - $c2);
                    $uip = str_replace('::', $fill, $uip);
                } else {
                    if ($c2 === -1) {
                        $fill = str_repeat(':0', 7 - $c1);
                        $uip = str_replace('::', $fill, $uip);
                    } else {
                        $fill = str_repeat(':0:', 6 - $c2 - $c1);
                        $uip = str_replace('::', $fill, $uip);
                        $uip = str_replace('::', ':', $uip);
                    }
                }
            }
        }
        return $uip;
    }

Usage Example

 public static function SplitV64($ip)
 {
     $ip = SimplePie_Net_IPv6::Uncompress($ip);
     if (strstr($ip, '.')) {
         $pos = strrpos($ip, ':');
         $ip[$pos] = '_';
         $ipPart = explode('_', $ip);
         return $ipPart;
     } else {
         return array($ip, '');
     }
 }