2

I'm trying to get the domain name and TLD (no subdomain) from a user-input URL string which may or may not have protocol, directories, subdomains, filenames, etc.

In other words, given any of the following:

example.com
www.example.com
sub.example.com
example.com/whatever/hey.html
http://example.com
https://subdomain.example.com
ftp://example.com/whatever/hey.html

I should always end up with: example.com.

Right now this is what I am doing:

$hostParts = explode('.', parse_url($URL, PHP_URL_HOST));
$tld = array_pop($hostParts);
$domain = array_pop($hostParts);
$domain = $domain . "." . $tld;

However, if a URL is provided without the protocol, it breaks. Why is parse_url failing to get the host in this situation?

brentonstrine
  • 21,694
  • 25
  • 74
  • 120

1 Answers1

3

By definition a URL contains a protocol or scheme. Check for // and if not present prepend // to the string. This may be different in PHP < 5.4.7 so maybe add http:// if no protocol.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • 1
    I can see that you're right. Here's the code I used to do it: `if(strpos($URL,"://")===false && substr($URL,0,1)!="/"){ $URL = "http://" . $URL; }` Just put that before the first line of the code, and it works. – brentonstrine Oct 25 '13 at 22:00
  • 1
    Wee bit simpler, perhaps: `$url = ( strpos( $urlString, "//" ) === false ) ? "//$urlString" : $urlString;` – Nathan Arthur Oct 13 '16 at 16:28