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

Deep-sea Anglerfish Black Seadevil Scary looking creature Video

Deep-sea Anglerfish are the strange and elusive creature that are very rarely observed in their natural habitat. Fewer than half a dozen have ever been captured on film or video by deep-diving research vehicles.They are mostly  found in tropical to temperate waters of the Indian,Pacific and Atlantic Oceans.

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...

Cheap Tourist boat service in Alapuzha - Enjoy the beauty of Aleppey Back waters using govt owned Tourist boat services

Feasible Tourist boat services in Alapuzha Aleppey / Alappuzha is one of the beautiful places in kerala. Aleppey district is famous for it amazing backwaters , you can enjoy house boat rides here. Kerala State Water transport department  (Kerala SWTD) provides facilities for tourists to enjoy the beauty of backwater by paying much less charge when compared to private boat services. There are boat services from Aleppey boat station which takes tourists to inner parts of the backwaters.


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