WpOrg\Requests

Ipv6::uncompress()public staticWP 1.0

Uncompresses an IPv6 address

RFC 4291 allows you to compress consecutive zero pieces in an address to '::'. This method expects a valid IPv6 address and expands the '::' to the required number of zero pieces.

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

Method of the class: Ipv6{}

No Hooks.

Return

String. The uncompressed IPv6 address

Usage

$result = Ipv6::uncompress( $ip );
$ip(string|Stringable) (required)
An IPv6 address

Ipv6::uncompress() code WP 6.6.2

public static function uncompress($ip) {
	if (InputValidator::is_string_or_stringable($ip) === false) {
		throw InvalidArgument::create(1, '$ip', 'string|Stringable', gettype($ip));
	}

	$ip = (string) $ip;

	if (substr_count($ip, '::') !== 1) {
		return $ip;
	}

	list($ip1, $ip2) = explode('::', $ip);
	$c1              = ($ip1 === '') ? -1 : substr_count($ip1, ':');
	$c2              = ($ip2 === '') ? -1 : substr_count($ip2, ':');

	if (strpos($ip2, '.') !== false) {
		$c2++;
	}

	if ($c1 === -1 && $c2 === -1) {
		// ::
		$ip = '0:0:0:0:0:0:0:0';
	} elseif ($c1 === -1) {
		// ::xxx
		$fill = str_repeat('0:', 7 - $c2);
		$ip   = str_replace('::', $fill, $ip);
	} elseif ($c2 === -1) {
		// xxx::
		$fill = str_repeat(':0', 7 - $c1);
		$ip   = str_replace('::', $fill, $ip);
	} else {
		// xxx::xxx
		$fill = ':' . str_repeat('0:', 6 - $c2 - $c1);
		$ip   = str_replace('::', $fill, $ip);
	}

	return $ip;
}