Skip to main content

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


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)); ?>

Popular posts from this blog

Strange problem occured while trying to create a CSV file using PHP Script - The file is not seen on FTP but can download using file's absolute path url

Strange problem occured while trying to create a CSV file - The file is not seen on FTP but can download using file's absolute path url Last day I came across a strange problem when I tried to create a csv file on therver using a PHP script. the script was simply writing a given content as a csv file. The file will be created runtime. What happened was, The script executed fine, file handler for new file was created and contents was wrote into the file using fwrite and it returned the number of bytes that was written.

How to get the Query string of a URL using the Javascript (JS)?

JS function get the Query string of a URL or value of each parameter using the Javascript(JS)? If you want to get your current page's url var my_url=document.location; to get the query string part of the url use like this: var my_qry_str= location.search; this will return the part of the url starting from "?" following by query string Lets assume that your current page url is http://www.crozoom.com/2013/page.html?qry1=A&qry2=B then the location.search function will return " ?qry1=A&qry2=B " to exclue "?", do like this:


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 »
Member
Search This Blog