3 * Class to validate and to work with IPv6 addresses
6 * @subpackage Utilities
10 * Class to validate and to work with IPv6 addresses
12 * This was originally based on the PEAR class of the same name, but has been
16 * @subpackage Utilities
21 * Uncompresses an IPv6 address
23 * RFC 4291 allows you to compress consecutive zero pieces in an address to
24 * '::'. This method expects a valid IPv6 address and expands the '::' to
25 * the required number of zero pieces.
27 * Example: FF01::101 -> FF01:0:0:0:0:0:0:101
28 * ::1 -> 0:0:0:0:0:0:0:1
30 * @author Alexander Merz <alexander.merz@web.de>
31 * @author elfrink at introweb dot nl
32 * @author Josh Peck <jmp at joshpeck dot org>
33 * @copyright 2003-2005 The PHP Group
34 * @license http://www.opensource.org/licenses/bsd-license.php
35 * @param string $ip An IPv6 address
36 * @return string The uncompressed IPv6 address
38 public static function uncompress($ip)
42 if (substr_count($ip, '::') === 1)
44 list($ip1, $ip2) = explode('::', $ip);
51 $c1 = substr_count($ip1, ':');
59 $c2 = substr_count($ip2, ':');
61 if (strpos($ip2, '.') !== false)
66 if ($c1 === -1 && $c2 === -1)
68 $ip = '0:0:0:0:0:0:0:0';
73 $fill = str_repeat('0:', 7 - $c2);
74 $ip = str_replace('::', $fill, $ip);
79 $fill = str_repeat(':0', 7 - $c1);
80 $ip = str_replace('::', $fill, $ip);
85 $fill = ':' . str_repeat('0:', 6 - $c2 - $c1);
86 $ip = str_replace('::', $fill, $ip);
93 * Compresses an IPv6 address
95 * RFC 4291 allows you to compress consecutive zero pieces in an address to
96 * '::'. This method expects a valid IPv6 address and compresses consecutive
97 * zero pieces to '::'.
99 * Example: FF01:0:0:0:0:0:0:101 -> FF01::101
100 * 0:0:0:0:0:0:0:1 -> ::1
103 * @param string $ip An IPv6 address
104 * @return string The compressed IPv6 address
106 public static function compress($ip)
108 // Prepare the IP to be compressed
109 $ip = self::uncompress($ip);
110 $ip_parts = self::split_v6_v4($ip);
112 // Replace all leading zeros
113 $ip_parts[0] = preg_replace('/(^|:)0+([0-9])/', '\1\2', $ip_parts[0]);
115 // Find bunches of zeros
116 if (preg_match_all('/(?:^|:)(?:0(?::|$))+/', $ip_parts[0], $matches, PREG_OFFSET_CAPTURE))
120 foreach ($matches[0] as $match)
122 if (strlen($match[0]) > $max)
124 $max = strlen($match[0]);
129 $ip_parts[0] = substr_replace($ip_parts[0], '::', $pos, $max);
132 if ($ip_parts[1] !== '')
134 return implode(':', $ip_parts);
143 * Splits an IPv6 address into the IPv6 and IPv4 representation parts
145 * RFC 4291 allows you to represent the last two parts of an IPv6 address
146 * using the standard IPv4 representation
148 * Example: 0:0:0:0:0:0:13.1.68.3
149 * 0:0:0:0:0:FFFF:129.144.52.38
151 * @param string $ip An IPv6 address
152 * @return array [0] contains the IPv6 represented part, and [1] the IPv4 represented part
154 private static function split_v6_v4($ip)
156 if (strpos($ip, '.') !== false)
158 $pos = strrpos($ip, ':');
159 $ipv6_part = substr($ip, 0, $pos);
160 $ipv4_part = substr($ip, $pos + 1);
161 return array($ipv6_part, $ipv4_part);
165 return array($ip, '');
170 * Checks an IPv6 address
172 * Checks if the given IP is a valid IPv6 address
174 * @param string $ip An IPv6 address
175 * @return bool true if $ip is a valid IPv6 address
177 public static function check_ipv6($ip)
179 $ip = self::uncompress($ip);
180 list($ipv6, $ipv4) = self::split_v6_v4($ip);
181 $ipv6 = explode(':', $ipv6);
182 $ipv4 = explode('.', $ipv4);
183 if (count($ipv6) === 8 && count($ipv4) === 1 || count($ipv6) === 6 && count($ipv4) === 4)
185 foreach ($ipv6 as $ipv6_part)
187 // The section can't be empty
188 if ($ipv6_part === '')
191 // Nor can it be over four characters
192 if (strlen($ipv6_part) > 4)
195 // Remove leading zeros (this is safe because of the above)
196 $ipv6_part = ltrim($ipv6_part, '0');
197 if ($ipv6_part === '')
200 // Check the value is valid
201 $value = hexdec($ipv6_part);
202 if (dechex($value) !== strtolower($ipv6_part) || $value < 0 || $value > 0xFFFF)
205 if (count($ipv4) === 4)
207 foreach ($ipv4 as $ipv4_part)
209 $value = (int) $ipv4_part;
210 if ((string) $value !== $ipv4_part || $value < 0 || $value > 0xFF)