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

Payback Points - How to redeem - How to merge multiple payback accounts - Block Payback card - Payback customer care

How to apply for a new ration card and what are the documents required?

List of documents required for new ration card application in Kerala and how to apply


Application for a new ration card should be addressed to Taluk Supply Officer (TSO) / City Rationing Officer (CRO) of applicant's residing area.

Primary document required are Residence certificate, Income certificate and incase the applicant's name is in another ration card then he/she should submit the reduction certificate ontained from previous TSO / CRO as proof for removing his/her name from old Ration card.

You can get the new application from your currently residing Taluk Supply Office.

Your SBI Debit card ending with XX0000 is deactivated only for Internet txn.

SBI account holders may have received an SMS with following message, supposed to be from State Bank of India (SBI).

Your SBI Debit card ending with XX0000  is deactivated only for Internet txn. To activate send SMS "SWON ECOM 0000" to 09223966666. No change for ATM/POS usage
** Replace the four Zeros with last 4 digits of your debit card number

Recently many of the SBI account holder has losed their money due to a hi-tech ATM robbery which happened in Thiruvananthapuram, capital city of Kerala.


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