0

Possible Duplicate:
Get the subdomain from a URL

I have seen posts about using parse_url to get www.domain.tld but how can i just get "domain" using php?

i have got this regex currently

$pattern = '@https?://[a-z]{1,}\.{0,}([a-z]{1,})\.com(\.[a-z]{1,}){0,}@';

but this only works with .com and i need it to work with all TLDs (.co.uk, .com , .tv etc.)

Is there a reliable way to do this, i am not sure if regex is the best way to go or not? or maybe explode on "." but then again subdomains would mess it up.

EDIT

so the desired outcome would be

$url = "https://stackoverflow.com/questions/11952907/get-domain-without-tld-using-php#comment15926320_11952907";

$output = "stackoverflow";

Doing more research would anyone advise using parse_url to get www.domain.tld then using explode to get domain?

Community
  • 1
  • 1
user1527354
  • 195
  • 2
  • 11

3 Answers3

2

Try this regex :

#^https?://(www\.)?([^/]*?)(\.co)?\.[^.]+?/#
Oussama Jilal
  • 7,669
  • 2
  • 30
  • 53
1

You could use the parse_url function. Doc is here.

Something like:

$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));

And then you can take $url['host'] and do:

$arr = explode('.',$url['host']);
return $arr[count($arr) - 2];
Eduard Luca
  • 6,514
  • 16
  • 85
  • 137
  • Try using it with a valid url and you see it is not the desired outcome. Array ( [scheme] => http [host] => www.google.com [path] => /efaafafaf.php [query] => id=78abak ) – user1527354 Aug 14 '12 at 13:14
  • Yes, you can if you get the `[host]` and `explode()` it and take the 2nd element from the tail. – Eduard Luca Aug 14 '12 at 14:11
  • 1
    Breaks for .co.uk or .org.br or similar. – cmc Mar 15 '13 at 13:16
0

I think you don't need regex.

function getDomain($url){
    $things_like_WWW_at_the_start = array('www');
    $urlContents = parse_url($url);
    $domain = explode('.', $urlContents['host']);

    if (!in_array($domain[0], $things_like_WWW_at_the_start))
        return $domain[0];
    else
        return $domain[1];
}
Leri
  • 12,367
  • 7
  • 43
  • 60
  • This will retrieve the complete hostname, ex `www.google.com`. The OP wants to get the TLD only (`google` in this example) – Tchoupi Aug 14 '12 at 13:12
  • This seems good, what other things would you suggest i put in the array to make it defiantly work cross urls? – user1527354 Aug 14 '12 at 13:34
  • @user1527354 Since many websites use subdomains to handle locale versions, I would add common language codes: `en`, `fr`, `de`, ... – Tchoupi Aug 14 '12 at 13:39
  • @user1527354 But if you have urls like stackexchange sites things will get really bad with this function. I mean you have to list everything in that array. – Leri Aug 14 '12 at 13:40