3v4l.org

run code in 300+ PHP versions simultaneously
<?php $values = [ 'ip' => '127.0.0.1', 'hostname' => 'localhost', 'ipport' => '127.0.0.1:3307', 'hostname' => 'localhost:3308', 'ipv6' => '[2001:0DB8::1234]', 'ipv6port' => '[2001:0DB8::1234]:3309', ]; foreach($values as $k=>$hostport) { $host = 'not found'; $port = 'not found'; // has port or ipv6 if (strpos($hostport, ':')) { // ipv6 if (str_starts_with($hostport, '[')) { $portposition = strpos($hostport,']:'); if ($portposition) { $port = substr($hostport, $portposition+2); $host = substr($hostport, 0, $portposition+1); } else { $host = $hostport; } } else { [$host, $port] = explode(':', $hostport); } } else { $host = $hostport; // ipv4/domainname without port } echo $k .': Hostname: ' . $host . ' Port: ' . $port ."\n"; }
Output for 8.1.0 - 8.1.29, 8.2.0 - 8.2.23, 8.3.0 - 8.3.11
ip: Hostname: 127.0.0.1 Port: not found hostname: Hostname: localhost Port: 3308 ipport: Hostname: 127.0.0.1 Port: 3307 ipv6: Hostname: [2001:0DB8::1234] Port: not found ipv6port: Hostname: [2001:0DB8::1234] Port: 3309

preferences:
51.31 ms | 406 KiB | 5 Q