@ 2013-10-24T14:08:54Z <?php
function normalise_ip($address)
{
if (preg_match('/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/', $address, $matches)) { // IPv4 address
$num = 0;
for ($i = 1; $i <= 4; $i++) {
$octet = (int) $matches[$i];
if ($octet > 255) {
return false;
}
$num |= $octet << ((4 - $i) * 8);
}
return sprintf('0000000000000000ffff%04x', $num);
} else if (preg_match('/\[?((?:[0-9a-f]{1,4}:|::)+[0-9a-f]{1,4})]?/i', strtolower($address), $matches)) { // IPv6 address
$parts = explode(':', $matches[1]);
$numParts = count($parts);
if ($part[0] === '') {
array_splice($parts, 0, 2, array_fill(0, 8 - ($numParts - 2), '0000'));
} else if ($parts[$numParts - 1] === '') {
array_splice($parts, $numParts - 2, 2, array_fill(0, 8 - ($numParts - 2), '0000'));
} else {
foreach ($parts as $i => $part) {
if ($part === '') {
array_splice($parts, $i, 1, array_fill(0, 8 - ($numParts - 1), '0000'));
}
}
}
foreach ($parts as &$part) {
$part = sprintf('%04s', $part);
}
return implode('', $parts);
} else { // invalid
return false;
}
}
echo normalise_ip('192.168.0.1') . "\n";
echo normalise_ip('[::ffff:C0A8:0001]') . "\n";
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.3.0 - 8.3.6 0000000000000000ffffc0a80001
Warning: Undefined variable $part in /in/fT4IB on line 18
Warning: Trying to access array offset on null in /in/fT4IB on line 18
00000000000000000000000000000000ffffc0a80001
Output for 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 0000000000000000ffffc0a80001
Warning: Undefined variable $part in /in/fT4IB on line 18
Warning: Trying to access array offset on value of type null in /in/fT4IB on line 18
00000000000000000000000000000000ffffc0a80001
Output for 7.4.0 - 7.4.25 , 7.4.27 - 7.4.33 0000000000000000ffffc0a80001
Notice: Undefined variable: part in /in/fT4IB on line 18
Notice: Trying to access array offset on value of type null in /in/fT4IB on line 18
00000000000000000000000000000000ffffc0a80001
Output for 7.3.32 - 7.3.33 , 7.4.26 0000000000000000ffffc0a80001
00000000000000000000000000000000ffffc0a80001
Output for 5.0.4 - 5.0.5 , 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 , 5.3.0 - 5.3.29 , 5.4.0 - 5.4.45 , 5.5.24 - 5.5.35 , 5.6.8 - 5.6.21 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.31 0000000000000000ffffc0a80001
Notice: Undefined variable: part in /in/fT4IB on line 18
00000000000000000000000000000000ffffc0a80001
Output for 5.0.0 - 5.0.3 0000000000000000ffffc0a80001
Notice: Undefined variable: part in /in/fT4IB on line 18
00000000000000000000000000000000ffffc0a80001
Output for 4.4.2 - 4.4.9 Parse error: syntax error, unexpected '&', expecting T_VARIABLE or '$' in /in/fT4IB on line 29
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 Parse error: parse error, unexpected '&', expecting T_VARIABLE or '$' in /in/fT4IB on line 29
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error, expecting `T_VARIABLE' or `'$'' in /in/fT4IB on line 29
Process exited with code 255 . preferences:dark mode live preview
227.34 ms | 401 KiB | 340 Q