Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Getting Domain Name From Subdomain

Tags:

php

subdomain

dns

I need to write a function to parse variables which contain domain names. It's best I explain this with an example, the variable could contain any of these things:

here.example.com example.com example.org here.example.org 

But when passed through my function all of these must return either example.com or example.co.uk, the root domain name basically. I'm sure I've done this before but I've been searching Google for about 20 minutes and can't find anything. Any help would be appreciated.

EDIT: Ignore the .co.uk, presume that all domains going through this function have a 3 letter TLD.

like image 824
zuk1 Avatar asked Jul 29 '09 15:07

zuk1


People also ask

How can I get sub domain in PHP?

You can do this by first getting the domain name (e.g. sub.example.com => example.co.uk) and then use strstr to get the subdomains.

Is a subdomain a FQDN?

A FQDN is a domain name that includes a host name, a root domain and a top level domain. It may also include additional subdomains between the root domain and the host name. Almost all individual parts of a FQDN are technically subdomains. The only part of a domain that isn't a subdomain is the root domain.


1 Answers

Stackoverflow Question Archive:

  • How to get domain name from url?
  • Check if domain equals value?
  • How do I get the base url?

print get_domain("http://somedomain.co.uk"); // outputs 'somedomain.co.uk'  function get_domain($url) {   $pieces = parse_url($url);   $domain = isset($pieces['host']) ? $pieces['host'] : '';   if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {     return $regs['domain'];   }   return false; } 
like image 187
Sampson Avatar answered Sep 21 '22 01:09

Sampson