Skip to content

Core\Uri::tryParse

Core\Uri::tryParse(string $uri): ?Core\UriCore\Uri::tryParse(
    string $uri,
): ?Core\Uri

Reads the text of an address and returns a Uri, the same way Core\Uri::parse does. The two methods are different only when the text is not an address. parse throws a RuntimeError, and tryParse returns null.

The result is null for exactly the texts that parse does not accept. A space, a control character, a % without two hex digits and a port above 65535 are some of them.

A relative link, such as /docs/start, is an address, so the result is a Uri. To check if an address is absolute, test that its scheme is not null: tryParse($text)?->scheme() != null.

The examples below check the text a user typed, test if a link is absolute, and check the address a login page sends the user to.

Parameters

  • $uristringThe text to read. A relative link, such as /a?b, is allowed.

Return value

Returns ?Core\Uri. A Uri. The result is null for every text that Core\Uri::parse throws an error for. To check if an address is absolute, use tryParse($text)?->scheme() != null.

Examples

Check text a user typed

<?nvs
// Shows how to check if a text is an address, without catching an error.
array<string> $typed = [
'https://example.com/my-files',
'https://example.com/my files',
'https://example.com:99999/',
];
// The second text has a space, and the third has a port above 65535.
// This prints the host of the first text, then "not an address" twice.
foreach ($typed as string $text) {
?Core\Uri $uri = Core\Uri::tryParse($text);
if ($uri == null) {
echo 'not an address', "\n";
} else {
echo $uri->host(), "\n";
}
}
Output
example.com
not an address
not an address

Is the link absolute

<?nvs
// Shows how to check if a link is absolute, which means it has a scheme such as `https`.
array<string> $links = ['https://example.com/start', '/docs/start', 'https://example.com/a b'];
// `?->` gives `null` when `tryParse` returns `null`.
// This prints "absolute", then "not absolute" twice.
foreach ($links as string $link) {
bool $absolute = Core\Uri::tryParse($link)?->scheme() != null;
echo $absolute ? 'absolute' : 'not absolute', "\n";
}
Output
absolute
not absolute
not absolute

Check where a login page sends the user

<?nvs
// Shows how a login page checks the address it sends the user to after they log in.
class AfterLogin
{
public static function target(string $next): string
{
?Core\Uri $uri = Core\Uri::tryParse($next);
// Text that is not an address goes to the start page.
if ($uri == null) {
return '/';
}
// An address with a scheme or a host can lead to another site, so it goes to the start page too.
if ($uri->scheme() != null || $uri->host() != null) {
return '/';
}
return $uri->toString();
}
}
// This prints /orders?page=2, then / three times.
array<string> $asked = [
'/orders?page=2',
'https://evil.example.net/login',
'//evil.example.net/login',
'/orders%zz',
];
foreach ($asked as string $next) {
echo AfterLogin::target($next), "\n";
}
Output
/orders?page=2
/
/
/