Core\Uri::parse
Reads the text of an address and returns a Uri. An address such as https://shop.example.com:8443/cart?sort=price#total has up to seven parts: the scheme, the user, the host, the port, the path, the query and the fragment. Each part has its own method, such as host() or query().
Every part is returned as it was written. Escapes such as %20 stay in the text, and upper-case letters stay upper case. A relative link such as /docs/start is allowed, and its scheme and host are null.
Core\Uri::parse throws a RuntimeError when the text is not an address. A space, a control character or a % without two hex digits all cause this. Core\Uri::tryParse returns null instead.
The examples below print each part of an address, show a relative link and text that is not an address, and read a database setting.
Parameters
$uristringThe text to read. A relative link, such as/a?b, is allowed. Its scheme isnull.
Return value
Returns Core\Uri. A Uri. Its methods return the parts of the address as they were written.
Errors
RuntimeErrorThe text is not an address under RFC 3986 (the standard for addresses). A space, a control character, a non-ASCII character, a%without two hex digits and a host in brackets that is not an IPv6 address all cause this. A port larger than65535causes it too. The message does not repeat the text, because the text can contain a password.
Examples
The parts of an address
<?nvs// Shows how to read an address and print each of its parts.
var $uri = Core\Uri::parse('https://anna@shop.example.com:8443/cart/items?sort=price#total');
// This prints https, anna, shop.example.com and 8443.echo $uri->scheme(), "\n";echo $uri->userInfo(), "\n";echo $uri->host(), "\n";echo $uri->port(), "\n";
// This prints /cart/items, sort=price and total.echo $uri->path(), "\n";echo $uri->query(), "\n";echo $uri->fragment(), "\n";
// Each part is returned as it was written.// The escape `%20` and the upper-case letters stay. This prints HTTP, Example.COM and /My%20Files.var $written = Core\Uri::parse('HTTP://Example.COM/My%20Files');echo $written->scheme(), ' ', $written->host(), ' ', $written->path(), "\n";httpsannashop.example.com8443/cart/itemssort=pricetotalHTTP Example.COM /My%20FilesA relative link and text that is not an address
<?nvs// Shows what `parse` returns for a link without a scheme, and for text that is not an address.
// A relative link has no scheme and no host, so both are `null`. This prints true twice.var $link = Core\Uri::parse('/docs/start?page=2');echo $link->scheme() == null ? 'true' : 'false', "\n";echo $link->host() == null ? 'true' : 'false', "\n";
// The path and the query are still there. This prints /docs/start and page=2.echo $link->path(), "\n";echo $link->query(), "\n";
// An address may not contain a space. For that text, `parse` throws a `RuntimeError`.// This prints /my-files, then "not an address".array<string> $typed = ['https://example.com/my-files', 'https://example.com/my files'];foreach ($typed as string $text) { try { echo Core\Uri::parse($text)->path(), "\n"; } catch (RuntimeError $e) { echo 'not an address', "\n"; }}truetrue/docs/startpage=2/my-filesnot an addressRead a database address from a setting
<?nvs// Shows how a program reads the server, the port, the user and the database name from one setting.
class DatabaseSetting{ public static function describe(string $setting): string { var $uri = Core\Uri::parse($setting); // The user name comes before the `:` in the user part. string $user = Core\Str::split($uri->userInfo() ?? '', ':')[0]; // The database name is the path without its first `/`. string $database = Core\Str::slice($uri->path(), 1); // The port is `null` when the setting does not give one, so 5432 is used. int $port = $uri->port() ?? 5432; return $user . ' on ' . ($uri->host() ?? '') . ':' . $port . ', database ' . $database; }}
// In a real program, these come from an environment variable or a configuration file.// This prints the user, the server, the port and the database for each setting.echo DatabaseSetting::describe('postgres://shop:secret@db.example.com:6432/orders'), "\n";echo DatabaseSetting::describe('postgres://report@db.example.com/stats'), "\n";shop on db.example.com:6432, database ordersreport on db.example.com:5432, database stats