WpOrg\Requests

Ipv6::compress()public staticWP 1.0

Compresses an IPv6 address

RFC 4291 allows you to compress consecutive 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

Метод класса: Ipv6{}

Хуков нет.

Возвращает

Строку. The compressed IPv6 address

Использование

$result = Ipv6::compress( $ip );
$ip(строка) (обязательный)
An IPv6 address

Заметки

  • Смотрите: \WpOrg\Requests\Ipv6::uncompress()

Код Ipv6::compress() WP 6.6.2

public static function compress($ip) {
	// Prepare the IP to be compressed.
	// Note: Input validation is handled in the `uncompress()` method, which is the first call made in this method.
	$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);
	} else {
		return $ip_parts[0];
	}
}