lithium\net\HostString::parse()

public static method

Parses host string that can either hold just the host name (i.e. localhost), a host/port combination (i.e. localhost:8080) or just the port prefixed with a colon (i.e. :8080). Also works with IPv4 and IPv6 addresses.

Note: IPv6 addresses must be enclosed in square brackets '[::1]:80'.

Parameters

  • string $host

    The host string with host, host/port or just port.

Returns

array

An associative array containing parsed 'host', 'port' or both.

Source

	public static function parse($host) {
		if ($host[0] === ':') {
			return ['port' => (integer) substr($host, 1)];
		}
		if ($host[0] === '[') {
			if (($close = strpos($host, ']')) === false) {
				throw new LogicException("Failed to parse host string `{$host}`.");
			}
			if (strlen($host) > $close + 1) {
				if ($host[$close + 1] !== ':') {
					throw new LogicException("Failed to parse host string `{$host}`.");
				}
				return [
					'host' => substr($host, 1, $close - 1),
					'port' => (integer) substr($host, $close + 2)
				];
			}
			return ['host' => substr($host, 1, -1)];
		}
		if (($colon = strpos($host, ':')) !== false) {
			return [
				'host' => substr($host, 0, $colon),
				'port' => (integer) substr($host, $colon + 1)
			];
		}
		return ['host' => $host];
	}