Below is a PHP function which finds the domain of a given url.
/*
This function takes a url as a parameter and returns the domain of the url.
This function also takes care of subdomains
Example - if given http://www.exmaple.com/example?sid=1@dd4 this function would return example.com
CAUTION- This function does NOT Validates a Url
*/
function domain($url)
{
$temp = parse_url($url);
if( ! isset($temp['scheme']))
{
$url='http://'.$url;
$temp=parse_url($url);
}
if(!empty($temp['host']))
{
$urls = $temp['host'];
$urls = str_ireplace("www.", "", $temp['host']);
return $urls;
}
else
{
if(!empty($temp['path']))
{
$url = $temp['path'];
$url = str_ireplace("www.", "", $temp['path']);
return $url;
}
}
}
Comments
Post a Comment