/** * Wrapper around Jeremy Kendall's PHP Domain Parser that parses the * domain/url passed to the function and returns the Tld and Valid domain * @param $domain * @throws \InvalidArgumentException when the tld is not valid * @return array returns an associative array with the domain and tld */ private function parse($domain) { $pslManager = new PublicSuffixListManager(); $parser = new Parser($pslManager->getList()); // First check if the suffix is actually valid if (!$parser->isSuffixValid($domain)) { throw new \InvalidArgumentException("Invalid TLD"); } $components = []; $components["tld"] = $parser->getPublicSuffix($domain); $components["domain"] = $parser->getRegisterableDomain($domain); return $components; }
$pslManager = new PublicSuffixListManager(); $parser = new Parser($pslManager->getList()); // Parse a URL $url = $parser->parseUrl('http://*****:*****@www.pref.okinawa.jp:8080/path/to/page.html?query=string#fragment'); // Accessing elements of the URL var_dump($url); var_dump($url->__toString()); var_dump($url->getPath()); var_dump($url->getFragment()); // Getting the Host object from the URL $host = $url->getHost(); // Accessing elements of the Host var_dump($host); var_dump($host->__toString()); var_dump($host->getSubdomain()); var_dump($host->getRegistrableDomain()); var_dump($host->getPublicSuffix()); // It's possible to parse a host only, if you prefer $host = $parser->parseHost('a.b.c.cy'); // Accessing elements of the Host var_dump($host); var_dump($host->__toString()); var_dump($host->getSubdomain()); var_dump($host->getRegistrableDomain()); var_dump($host->getPublicSuffix()); // If you just need to know subdomain/registrable domain/public suffix info // about a host, there are public methods available for that in the Parser var_dump($parser->getSubdomain('www.scottwills.co.uk')); var_dump($parser->getRegistrableDomain('www.scottwills.co.uk')); var_dump($parser->getPublicSuffix('www.scottwills.co.uk'));