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

How to use WiFi adapter on Ubuntu 16.04 desktop PC - Realtek RTL8188EUS 802.11n Wireless USB Network Adapter Driver installation

Installation of Realtek RTL8188EUS 802.11n Wireless USB Network Adapter on Desktop PC having Ubuntu 16.04 OS My PC is running in Ubuntu 16.04 OS, recently I thought of using a dongle wife adapter to access our home's Wifi network. For this I used Realtek RTL8188EUS 802.11n Wireless USB Network Adapter ( Model No: OT-WUA950NM ) This small device cost around Rupees 250/- to Rs 300/- in India. I did the following steps for installation of this Realtek Nano Wifi Adapter: Plug Realtek RTL8188EUS 802.11n Wireless USB Network Adapter to your PC's USB port, Take the terminal application and run the command "lsusb" to list the plugged in usb devices: Myhome:~$ lsusb Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 001 Device 003: ID 0bda:8179 Realtek Semiconductor Corp. R...

How to delete videos from your Youtube Watch History list?

How to Delete Individual or all videos from your Youtube Watch History list? Youtube keeps a fine record of the videos that you had watched earlier. You can view this by visiting the History section. If you want to remove the video's from the list do the following: Logon to Youtube and click on the "History" tab on the left menu to view Watch History ( Read more ) There will be check boxes corresponding to each video in the list Tick the check boxes of the videos which you want to remove Click on " Remove " button to delete the videos.

ICICI prudential Customer portal updated - Option to change password is missing - Know how to change your ICICI prudential password

Recently I received an SMS from ICICI prudential asking for login to their website's customer portal using the phone number as user Id and an autogenerated one time password given in the message as password. The SMS messsage was like this. Dear ***Cust Name*** login to your policy(ies) on www.iciciprulife.com with your user id as **mobile number*** and One time use password as ***password***


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