How to strip domain name out of a url in PHP?
parse_u开发者_如何学编程rl($url, PHP_URL_HOST)
The above will work for:
sub1.domain.name
,
sub2.domain.name
,...
I want to strip the domain name only.
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.
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;
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.
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.
精彩评论