I need to get the domain name from an URL. The following examples should all return google.com:
google.com
images.google.com
new.images.google.com
www.google.com
Similarly the following URLs should all return google.co.uk.
google.co.uk
images.google.co.uk
new.images.google.co.uk
http://www.google.co.uk
I’m hesitant to use Regular Expressions, because something like domain.com/google.com could return incorrect results.
How can I get the top-level domain, using PHP? This needs to work on all platforms and hosts.
You could do this:
** Update **
The best way I can think of is to have a mapping of all the TLDs that you want to handle, since certain TLDs can be tricky (co.uk).