Forum Moderators: coopster
<?php$url = "http://somesubdomain.someotherstuff.abc.com/somedir";
$data = parse_domainname($url);
print_r($data);
function parse_domainname($url)
{
if (preg_match('#([a-z]+)://(.*?)\.(.*?)\.(.*?)\.([a-z]+)#is', $url, $data))
{
// http://a.b.domain.com /path
return $data;
}
else if (preg_match('#([a-z]+)://(www\.)?(.*?)\.(.*?)\.([a-z]+)#is', $url, $data))
{
// http://a.domaim.com /path
// http://www.a.domain.com /path
return $data;
}
else if (preg_match('#([a-z]+)://(www\.)?(.*?)\.([a-z]+)#is', $url, $data))
{
// http://domain.com /path
// http://www.domain.com /path
return $data;
}
}
?>
Which is your result of the array, returned by the function?
The preg_match in the function removes the string after tld (.com)
* My returned array is:
Array ( [0] => [somesubdomain.someotherstuff.abc.com...] => http [2] => somesubdomain [3] => someotherstuff [4] => abc [5] => com ) [1][edited by: Psychopsia at 12:18 am (utc) on Aug. 31, 2006]
$url = parse_url('http://subdomain.somehost.com');
$url = explode('.',$url['host']);
if(count($url)>1) {
$url = $url[count($url)-2];
} else {
$url = $url[0];
}
echo $url; //this prints "somehost"
It works with in all cases, even in "localhost"