Requests_IPv6::compress()

最后更新于:2021-11-26 03:45:19

Requests_IPv6::compress( string$ip)

Compresses an IPv6 address

参数

$ip

(string) (Required) An IPv6 address

响应

(string) The compressed IPv6 address

源文件

文件: gc-includes/Requests/IPv6.php

	public static function compress($ip) {
		// 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])/', '12', $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];
		}
	}