3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function ipv6_addr_in_subnet($addr, $subnet, $mask = null) { $addrBin = @inet_pton($addr); if (!$addrBin) { throw new \InvalidArgumentException('Invalid IPv6 address: ' . $addr); } $cidr = 0; if (isset($mask)) { if (is_int($mask)) { $cidr = (int)$mask; if ($cidr < 0 || $cidr > 128) { throw new \InvalidArgumentException('Invalid IPv6 CIDR mask: ' . $mask); } } else { $maskBin = @inet_pton($mask); if (!$maskBin) { throw new \InvalidArgumentException('Invalid IPv6 mask: ' . $mask); } } $subnetAddr = $subnet; } else { $parts = explode('/', $subnet); $numParts = count($parts); if ($numParts === 1) { $maskBin = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"; } else if ($numParts === 2) { $cidr = (int)$parts[1]; } else { throw new \InvalidArgumentException('Invalid IPv6 subnet'); } $subnetAddr = $parts[0]; } if (!isset($maskBin)) { $blocks = (int)($cidr / 16); $maskBin = pack( 'n*', ...array_fill(0, $blocks, 0xffff), ...array_pad([((2 ** (16 - ($cidr % 16))) - 1) ^ 0xffff], 8 - $blocks, 0) ); } $subnetAddrBin = @inet_pton($subnetAddr); if (!$subnetAddrBin) { throw new \InvalidArgumentException('Invalid IPv6 address: ' . $subnetAddr); } return ($addrBin & $maskBin) === ($subnetAddrBin & $maskBin); } var_dump(ipv6_addr_in_subnet('2001::1', '2001::/16')); var_dump(ipv6_addr_in_subnet('2001::1', '2000::/17')); var_dump(ipv6_addr_in_subnet('2001::1', '2001::/15')); var_dump(ipv6_addr_in_subnet('2001::1', '2001::/120'));
based on 792TV
Output for 5.6.7 - 7.2.0
bool(true) bool(false) bool(true) bool(true)
Output for 5.3.0 - 5.5.35
Parse error: syntax error, unexpected '.' in /in/T2jeX on line 45
Process exited with code 255.
Output for 5.1.0 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/T2jeX on line 7 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/T2jeX on line 16 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/T2jeX on line 21 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/T2jeX on line 35 Parse error: syntax error, unexpected '.' in /in/T2jeX on line 45
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/T2jeX on line 7 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/T2jeX on line 16 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/T2jeX on line 21 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/T2jeX on line 35 Parse error: parse error, unexpected '.' in /in/T2jeX on line 45
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_NEW in /in/T2jeX on line 7
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_NEW in /in/T2jeX on line 7
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/T2jeX on line 7
Process exited with code 255.