Skip to content

Core\Uri::compareTo

$uri->compareTo(Core\Uri $other): int$uri->compareTo(
    Core\Uri $other,
): int

Compares two addresses and returns -1, 0 or 1, so you can sort addresses or check if two of them are the same.

$a->compareTo($b) returns 0 when both are the same address, even when they are written in a different way. Before it compares them, it makes three changes to a copy of each address. The scheme and the host become lower case. An escape such as %7E becomes the plain character, here ~, when that character does not need an escape. The . and .. parts are removed from a path that starts with /. Neither address itself changes. In all other cases, the result is -1 when $a comes first and 1 when $b comes first.

Good to know: http://example.com:80/ and http://example.com/ are different here, because compareTo does not know which port each scheme uses by default. The path is case-sensitive, so /A and /a are different too.

The examples below check two ways to write one address, sort a list of addresses, and skip pages a program has already visited.

Parameters

  • $otherCore\UriThe Uri to compare with.

Return value

Returns int. 0 when both are the same address, -1 when this address comes first, and 1 when $other comes first. The parts are compared in this order: scheme, userInfo, host, port, path, query, fragment. A missing part comes before a part that is there. http://example.com:80/ and http://example.com/ are different, because the default port of a scheme is not known.

Examples

One address written two ways

<?nvs
// Shows how to check if two addresses are the same, even when they are written in a different way.
var $written = Core\Uri::parse('HTTP://Example.COM/docs/./guide%7Eone');
var $plain = Core\Uri::parse('http://example.com/docs/guide~one');
// The scheme and the host are compared in lower case. `%7E` is compared as `~`,
// and the `/./` part is removed. This prints 0.
echo $written->compareTo($plain), "\n";
// `compareTo` does not change either address. This prints the text as it was written.
echo $written->toString(), "\n";
// The path is case-sensitive, so `/Docs` is a different address. This prints 1.
var $upper = Core\Uri::parse('http://example.com/Docs/guide~one');
echo $plain->compareTo($upper), "\n";
Output
0
HTTP://Example.COM/docs/./guide%7Eone
1

Sort a list of addresses

<?nvs
// Shows how to sort a list of addresses.
array<string> $links = [
'https://example.com/b',
'http://example.com/z',
'https://example.com/a',
'http://example.com/a?page=2',
];
// The comparator parses both texts and returns what `compareTo` returns.
// The scheme is compared first, so every `http` address comes before every `https` address.
var $sorted = Core\Arr::sort($links, { comparator: fn(string $x, string $y): int => Core\Uri::parse($x)->compareTo(Core\Uri::parse($y)) });
// This prints the four addresses, one per line, in sorted order.
foreach ($sorted as string $link) {
echo $link, "\n";
}
Output
http://example.com/a?page=2
http://example.com/z
https://example.com/a
https://example.com/b

Skip pages already visited

<?nvs
// Shows how a link checker skips a page it has already visited, when the same page is linked in different ways.
class Visited
{
public static function contains(array<Core\Uri> $pages, Core\Uri $link): bool
{
foreach ($pages as Core\Uri $page) {
if ($page->compareTo($link) == 0) {
return true;
}
}
return false;
}
}
// In a real program, these links come from the pages the checker downloads.
array<string> $found = [
'https://example.com/shop/',
'https://EXAMPLE.com/shop/',
'https://example.com/blog/../shop/',
'https://example.com/shop/%63art',
'https://example.com/shop/cart',
];
array<Core\Uri> $pages = [];
foreach ($found as string $text) {
var $link = Core\Uri::parse($text);
if (Visited::contains($pages, $link)) {
echo 'skip ', $text, "\n";
} else {
$pages[] = $link;
echo 'visit ', $text, "\n";
}
}
// Five links were found, but only two different pages. This prints 2.
echo Core\Arr::count($pages), "\n";
Output
visit https://example.com/shop/
skip https://EXAMPLE.com/shop/
skip https://example.com/blog/../shop/
visit https://example.com/shop/%63art
skip https://example.com/shop/cart
2