<?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";
}
preferences:
31.18 ms | 407 KiB | 5 Q