tags:

views:

152

answers:

4
parse_url($url, PHP_URL_HOST)

The above will work for:

sub1.domain.name,

sub2.domain.name,...

I want to strip the domain name only.

A: 

You can do this in dozens of ways. It could look like this:

$host = 'en.sub.domain.name';
$subdomain = implode('.', explode('.', $host, -2));

EDIT:

This is not going to work with .co.uk, .com.pl, .gov.fr SLDs. It seems that the easiest way to remove domain is to create an array of TLDs and SLDs, loop through that array and if $host ends with some array's element then cut last n characters and break the loop.

Crozin
It won't work for hostname like this:`news.bbc.co.uk`
Add a special case for when it has more than two periods, then.
Alex JL
@Code Duck: what if we have more than one subdomain?
Crozin
I agree, that's what makes this so difficult. The plan to create a list of valid TLDs could be the only way to be sure.
Alex JL
It's not applicable,there can be third level domain,fourth level domains...no limitation.
There is a finite and well defined list of 1st and 2nd level domains.
Alex JL
+1  A: 

So what you are starting with is not a URL.

The easiest solution is to make it look like a URL:

function get_host_from_bad_url($url)
{
 if (!$candidate_host=parse_url($url, PHP_URL_HOST)) { // deliberate assignment
     $candidate_host=parse_url('http://' . $url, PHP_URL_HOST);
 }
 return $condidate_host;
}

There are all sorts of other things which might contain a host name - but without a lot more info its hard to suggest how to parse these.

However an alternative approach would be to try to extract anything which looks like it might be a hostname (and then potentially, do a DNS check on it):

function strip_hosts_from_string($inp)
{
 $inp=strtolower($inp);
 $matches=preg_match_all('/([a-z0-9\-]+\.){2,}([a-z]{2,6})/',$inp);
 $hostnames=$matches[0];
 foreach ($hostnames as $x=>$host) {
       if (gethostbyname($host)==$host) {
           unset($hostnames[$x]);
       }
 }
 return $hostnames;
}

C.

symcbean
+1,this is almost there.But I don't know if there is a more direct method.
A: 

Try this--it's a little 'wordy' but it does what you need with the information provided in comments and this will work for full URL including HTTP and without if you just have the domain.

$domain = 'domain1.domain2.bbc.co.uk';
$array = explode('.', $domain);
$trash = array_pop($array);
$trash = array_pop($array);

$subdomain = join('.', $array);
print $subdomain;
angryCodeMonkey
A: 

You could just take anything after the first dot, if there is more than one dot.

$url=parse_url($url, PHP_URL_HOST);
$domain=substr($url,strpos($url,'.')+1);

This is a bit hacky, of course, but it works on your examples. You might wish to test to see if there were at least two .s first. If you're dealing with a url like news.bbc.co.uk it would work, but not for bbc.co.uk.

Alex JL
It should work with **all** urls.
Well, good luck.
Alex JL