SimplePie_Net_IPv6::compress

Advertisement

Summery Summery

Compresses an IPv6 address

Syntax Syntax

SimplePie_Net_IPv6::compress( string $ip )

Description Description

RFC 4291 allows you to compress concecutive zero pieces in an address to ‘::’. This method expects a valid IPv6 address and compresses consecutive zero pieces to ‘::’.

Example: FF01:0:0:0:0:0:0:101 -> FF01::101 0:0:0:0:0:0:0:1 -> ::1

Parameters Parameters

$ip

(Required) An IPv6 address

Return Return

(string) The compressed IPv6 address

Source Source

File: wp-includes/SimplePie/Net/IPv6.php

	{
		// Prepare the IP to be compressed
		$ip = self::uncompress($ip);
		$ip_parts = self::split_v6_v4($ip);

		// Replace all leading zeros
		$ip_parts[0] = preg_replace('/(^|:)0+([0-9])/', '\1\2', $ip_parts[0]);

		// Find bunches of zeros
		if (preg_match_all('/(?:^|:)(?:0(?::|$))+/', $ip_parts[0], $matches, PREG_OFFSET_CAPTURE))
		{
			$max = 0;
			$pos = null;
			foreach ($matches[0] as $match)
			{
				if (strlen($match[0]) > $max)
				{
					$max = strlen($match[0]);
					$pos = $match[1];
				}
			}

			$ip_parts[0] = substr_replace($ip_parts[0], '::', $pos, $max);
		}

		if ($ip_parts[1] !== '')
		{
			return implode(':', $ip_parts);
		}

		return $ip_parts[0];
	}

	/**
	 * Splits an IPv6 address into the IPv6 and IPv4 representation parts

Advertisement

See also See also

Advertisement

Leave a Reply