Requests_IPv6::compress() WordPress Method
The IPv6 address compression algorithm is a method of representing an IPv6 address in a more concise format. The algorithm is specified in RFC 5952. The algorithm uses a series of rules to compress the address. For example, if an address contains a sequence of zeros, the zeros can be represented as a colon (:). The purpose of the algorithm is to make IPv6 addresses more readable and easier to work with. The algorithm is also used in the construction of IPv6 addresses in URLs.
Requests_IPv6::compress( string $ip ) #
Compresses an IPv6 address
Description
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
See also
Parameters
- $ip
(string)(Required)An IPv6 address
Return
(string) The compressed IPv6 address
Source
File: wp-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])/', '\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]; } }
Expand full source codeCollapse full source codeView on TracView on GitHub