3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function normalise_ip($address) { $address = trim(strtolower($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('00000000000000000000ffff%04x', $num); } else if (preg_match('/^\[?((?:[0-9a-f]{1,4}:?|::)+)]?$/i', $address, $matches)) { // IPv6 address $parts = explode(':', $matches[1]); $numParts = count($parts); var_dump($parts, $parts[$numParts - 1]); if ($parts[0] === '') { array_splice($parts, 0, 2, array_fill(0, 8 - ($numParts - 2), '0000')); } else if ($parts[$numParts - 1] === '') { echo "end\n"; 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('[C0A8:0001::]') . "\n";
Output for 5.0.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
00000000000000000000ffffc0a80001 array(5) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(4) "ffff" [3]=> string(4) "c0a8" [4]=> string(4) "0001" } string(4) "0001" 00000000000000000000ffffc0a80001 array(5) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(4) "ffff" [3]=> string(4) "c0a8" [4]=> string(1) "1" } string(1) "1" 00000000000000000000ffffc0a80001 array(5) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(4) "ffff" [3]=> string(4) "c0a8" [4]=> string(4) "0001" } string(4) "0001" 00000000000000000000ffffc0a80001 array(4) { [0]=> string(4) "c0a8" [1]=> string(4) "0001" [2]=> string(0) "" [3]=> string(0) "" } string(0) "" end c0a80001000000000000000000000000
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected '&', expecting T_VARIABLE or '$' in /in/vcVXG on line 33
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected '&', expecting T_VARIABLE or '$' in /in/vcVXG on line 33
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_VARIABLE' or `'$'' in /in/vcVXG on line 33
Process exited with code 255.