3v4l.org

run code in 300+ PHP versions simultaneously
<?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 git.master, git.master_jit, rfc.property-hooks
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

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
36.08 ms | 403 KiB | 8 Q