Home   Best Sellers   Blogging   Coding & Design   Technology   SEO   Travel & living   Career   Videos   Tips   Calculators     
Home  »     »     »     »     »  PHP function to return the domain or subdomain from a given url

PHP function to return the domain or subdomain from a given url

Monday, June 30, 2014


PHP code to return the domain name from a given url string



Here is a simple function which uses regular expression to find the domain name from a given url string.

Input to the function is the URL string.


Here is what the function does:

It check whether string is null. If null, function return false , Else it will pass the url string to a php builtin function parse_url(). This function parses the given URL and returns an associative array containing any of the various components of the URL that are present. Then the preg_match() is applied on "host" component of the component array,to grab the domain name.

Code is given below:

Function to return domain name in a url string

<?php

function get_domain_from_url($input_url)
{
if( trim($input_url)=="") return false
else{
$parts = parse_url($input_url);
$domain = isset($parts['host']) ? $parts['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
return $regs['domain'];
}else
return false;
}
}

// How to use the function?


$domain_name = get_domain_from_url("http://www.crozoom.com/2011/10/sub-domain-tracking-and-cross-domain.html");

echo $domain_name;

// this will output www.crozoom.com
?>




PHP Function to return all url components as an array
Inorder to retrive all the components (including file extension,domain name and subdomain name) as an array , we can use amodified version of above function. The unction will return an associative array with each element in the url.


<?php

function get_url_components($input_url)
{
if( trim($input_url)=="") return false
else{
$parts = parse_url($input_url);
$domain = isset($parts['host']) ? $parts['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
$parts["domain_name"]= $regs['domain'];
}

$dot_pos=strrpos($parts['path'],".");
if( $dot_pos!==false){
$dot_pos++;
$parts["file_extension"]=substr($parts['path'],$dot_pos);
}else
$parts["file_extension"]="";

return $parts;
}
}

// How to use the function?


$url_parts = get_url_components("http://www.c.mail.crozoom.com/2011/10/sub-domain-tracking-and-cross-domain.html?t=1");

print_r($url_parts);

// this will output as            Array ( [scheme] => http [host] => www.c.mail.crozoom.com [path] => /2011/10/sub-domain-tracking-and-cross-domain.html [query] => t=1 [domain_name] => crozoom.com [file_extension] => html )

// You can access subdomain name as
$subdomain = $url_parts['host'];

// You can access domain name as
$domain = $url_parts['domain_name'];

// You can access query string as
$query_str = $url_parts['query'];

// You can access file extension
$file_ext = $url_parts['file_extension'];

?>


PREG_MATCH()
Syntax: int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
Searches subject for a match to the regular expression given in pattern. If match found then $matches array is filled with the results of search. $matches[0] will contain the text that matched the full pattern. read more >>

PARSE_URL()
Syntax: mixed parse_url ( string $url [, int $component = -1 ] )
Function parses a given URL string and returns the components as an associative array. It will return FALSE for Seriously malformed urls. You can also pass some PHP constants ( PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY,PHP_URL_FRAGMENT or PHP_URL_PORT) as second parameter to retrive a specific component from url.read more>>

To get the domain name or subdomain name you can simply use like this:
<?php echo(parse_url($Your_Url_String,PHP_URL_HOST)); ?>




How to link to this page?
If you wish to link to this page from your website, simply Copy and paste the above HTML code to your web page. It will appear on your page as:
PHP function to return the domain or subdomain from a given url.




Share this!




comments powered by Disqus

This Weeks 7 Popular Posts


Subscribe to Recent Posts by Email
Stay connected to CROZOOM with regular Email notices of new Techie articles and IT Jobs. Updates will be delivered to your Inbox as soon as they are posted online.

Enter Your Email Address:  

Delivered by FeedBurner   RSS Feed

Search this Blog   



Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to recruit.vo@gmail.com   Read more »