3v4l.org

run code in 300+ PHP versions simultaneously
<?php function normalise_ip($address) { $address = trim(strtolower($address)); $ipv4Expr = '/^\[?(?:(?:(?:0{1,4}:){1,5}|:):?ffff:)?(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})]?$/i'; $ipv6Expr = '/^\[?((?:[0-9a-f]{1,4}:?|::)+)]?$/i'; if (preg_match($ipv4Expr, $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('00000000000000000000ffff%04x', $num); } else if (preg_match($ipv6Expr, $address, $matches)) { // IPv6 address $parts = explode(':', $matches[1]); $numParts = count($parts); if ($parts[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"; echo normalise_ip('::ffff:C0A8:1') . "\n"; echo normalise_ip('[::ffff:C0A8:0001]') . "\n"; echo normalise_ip('[::ffff:192.168.0.1]') . "\n"; echo normalise_ip('0:0:0:0:0:ffff:192.168.0.1') . "\n"; echo normalise_ip('[0:0:0:0:0:ffff:192.168.0.1]') . "\n";
Output for 5.0.0 - 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.0 - 5.5.38, 5.6.0 - 5.6.30, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.6 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
00000000000000000000ffffc0a80001 00000000000000000000ffffc0a80001 00000000000000000000ffffc0a80001 00000000000000000000ffffc0a80001 00000000000000000000ffffc0a80001 00000000000000000000ffffc0a80001 00000000000000000000ffffc0a80001
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected '&', expecting T_VARIABLE or '$' in /in/Qt34i on line 34
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/Qt34i on line 34
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_VARIABLE' or `'$'' in /in/Qt34i on line 34
Process exited with code 255.

preferences:
261.87 ms | 401 KiB | 399 Q