say someone enters a URL like this:
http://i.imgur.com/a/b/c?query=value&query2=value
And I want to return: imgur.com
not i.imgur.com
This is code I have right now
$sourceUrl = parse_url($url);
$sourceUrl = $sourceUrl['host'];
But this returns i.imgur.com
If you only want the domain name, try the following:
You need package that using Public Suffix List. Yes, you can use string functions arround parse_url() or regex, but they will produce incorrect result in complex URLs.
I recomend TLDExtract for domain parsing, here is sample code:
The code below should be perfect for the job.
Check the code below, it should do the job fine.
I have found a very useful library using publicsuffix.org, PHP Domain Parser is a Public Suffix List based domain parser implemented in PHP.
https://github.com/jeremykendall/php-domain-parser