Documentation for crwlr / url (v2.1)

Comparing Urls or Url Components

You may think to compare two urls you don't need this library, but whenever you have urls from an unpredictable input source, I'd recommend using it. Imagine a case like this:

$url1 = 'https://www.example.com/foo/bár/báz';
$url2 = 'https://www.example.com/foo/b%C3%A1r/b%C3%A1z';

var_dump($url1 === $url2);
// Returns false. Of course the two strings aren't equal.

var_dump(Url::parse($url1)->isEqualTo($url2));
// Returns true, because the path /foo/bár/báz is percent-encoded
// in the Url class to /foo/b%C3%A1r/b%C3%A1z

It's also really easy to compare the same component of two different urls.

$url1 = Url::parse('https://u:p@www.example.com/foo?q=s#frag');
$url2 = Url::parse('http://s:a@jobs.eggsample.org/bar?u=t#ment');
$url3 = clone $url1;

$url1->isSchemeEqualIn($url2); // false
$url1->isSchemeEqualIn($url3); // true

$url1->isHostEqualIn($url2); // false
$url1->isHostEqualIn($url3); // true

$url1->isPasswordEqualIn($url2); // false
$url1->isPasswordEqualIn($url3); // true

These are all available comparison methods:

  • isEqualTo($url)
  • isComponentEqualIn($url, $componentName)
  • isSchemeEqualIn($url)
  • isAuthorityEqualIn($url)
  • isUserEqualIn($url)
  • isPasswordEqualIn($url)
  • isUserInfoEqualIn($url)
  • isHostEqualIn($url)
  • isDomainEqualIn($url)
  • isDomainLabelEqualIn($url)
  • isDomainSuffixEqualIn($url)
  • isSubdomainEqualIn($url)
  • isPortEqualIn($url)
  • isPathEqualIn($url)
  • isQueryEqualIn($url)
  • isFragmentEqualIn($url)