Requests_IPv6::uncompress()

最后更新于:2021-11-26 03:53:20

Requests_IPv6::uncompress( string$ip)

Uncompresses an IPv6 address

参数

$ip

(string) (Required) An IPv6 address

响应

(string) The uncompressed IPv6 address

源文件

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

	public static function uncompress($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';
		}
		// ::xxx
		elseif ($c1 === -1) {
			$fill = str_repeat('0:', 7 - $c2);
			$ip   = str_replace('::', $fill, $ip);
		}
		// xxx::
		elseif ($c2 === -1) {
			$fill = str_repeat(':0', 7 - $c1);
			$ip   = str_replace('::', $fill, $ip);
		}
		// xxx::xxx
		else {
			$fill = ':' . str_repeat('0:', 6 - $c2 - $c1);
			$ip   = str_replace('::', $fill, $ip);
		}
		return $ip;
	}