i was working on a recent project and suddenly i wanted to extract domain name domain.tld stripping off www or http://
I know that parse_url() is the function that accomplishes most of the task. I have tested this function and if users type with www.domain.com then domain.com is only available with path array variable and not in host array variable whereas with http:// the domain.com comes in host array. This is a tricky problem when parsing url to get domain and this function overcomes this
For example:
http://domain.co.uk => gives => domain.co.uk
http://domain.co.uk/users/main.html => gives => domain.co.uk
http://www.domain.co.uk => gives => domain.co.uk
www.domain.co.uk => gives => domain.co.uk
function GetDomain($url)
{
$nowww = ereg_replace('www\.','',$url);
$domain = parse_url($nowww);
if(!empty($domain["host"]))
{
return $domain["host"];
} else
{
return $domain["path"];
}
}
The above function worked very well for me!
Method 2
Getting the domain name out of a URL using regular expressions. I accidentally found this code in php preg_match function
<?php
// get host name from URL
preg_match("/^(http:\/\/)?([^\/]+)/i",
"http://www.php.net/index.html", $matches);
$host = $matches[2];
// get last two segments of host name
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
echo "domain name is: {$matches[0]}\n";
/* Output is php.net */
?>
Hope this helps!
Similar Posts:
- Fix-> Warning: ereg_replace(): REG_BADRPT in PHP
- Using htaccess to redirect domain without http://
- How to filter & escape data from Injection attacks in PHP!
- How to remove GET variable within $_SERVER['QUERY_STRING'] in PHP
- Domain with WWW not resolving problem!
- How to redirect when post submitted for review in WordPress
- how to store and retrieve checkbox value in php?
- how to change hostname in linux centos?
- Affiliate Hide – Free wordpress plugin to redirect affiliate links!
- WordPress – how to add custom field when post is published?


March 5, 2009
Sorry, but neither of these functions really work.
On the first:
You can have subdomains that aren’t ‘www’. If I put in ‘http://sub.gulati.info’, I would expect the return of ‘gulati.info’, but your function gives me ‘sub.gulati.info’.
Here’s a better function:
function getDomain( $inputURL ) {$arrayURL = explode( '.', $inputURL );
$i = count( $arrayURL ) - 1;
if( strlen( $arrayURL[ $i ] )
This will work for everything except CC.CC urls (such as ab.us or something).
On the second:
It gives me a couple of regex errors.
Thanks!
March 5, 2009
function getDomain( $inputURL ) {$arrayURL = explode( '.', $inputURL );
$i = count( $arrayURL ) - 1;
if( strlen( $arrayURL[ $i ] ) <= 2 && strlen( $arrayURL[ $i - 1 ] ) <= 2 ) {
$cleanURL = $arrayURL[ $i - 2 ] . '.' .$arrayURL[ $i - 1 ] . '.' . $arrayURL[ $i ];
} else {
$cleanURL = $arrayURL[ $i - 1 ] . '.' . $arrayURL[ $i ];
}
return $cleanURL;
}
Correction on the code.
April 21, 2012
Parse error: syntax error, unexpected ‘=’ on line 6
June 23, 2009
The right one
June 23, 2009
// get host name from URL
preg_match(‘@^(?:http://)?([^/]+)@i’,
“http://www.php.net/index.html”, $matches);
$host = $matches[1];
// get last two segments of host name
preg_match(‘/[^.]+\.[^.]+$/’, $host, $matches);
echo “domain name is: {$matches[0]}\n”;
April 28, 2010
The below function worked very well:
function getDomain($url)
{
if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) === FALSE)
{
return false;
}
/*** get the url parts ***/
$parts = parse_url($url);
/*** return the host domain ***/
return $parts['scheme'].’://’.$parts['host'];
}
May 19, 2010
What company is the best Domain Registrar? i’ve heard that Godaddy and Moniker are the best.:`:
July 18, 2010
This domain extract function helped me for my academic project. Thanx.
November 24, 2010
hhhmm.. so who’s code is the “right” one?
January 9, 2011
None. They all assume that domain names look like example.com but completely disregard the fact that a whole lot of domains look like example.co.uk.
February 2, 2011
Could use just do a whois check?
March 25, 2011
if domain name end with www such as http://www.lawww.com or http://www.giwww.com, code is not working correctly…
April 25, 2011
Very nice post. This function is exactly what I am looking for.
Thanks
May 5, 2011
Spot on. Great post.
June 17, 2011
Very useful, thanks.
June 20, 2011
This function is exactly what I am looking for
August 9, 2011
Or you can simply use :
parse_url(“http://www.myurl.com”, PHP_URL_HOST);
Please sometimes read PHP Manual !
April 21, 2012
Hoping it will help me..
Thanks a Lot
August 13, 2011
>Guillaume
“Please sometimes read PHP Manual !”
Or you could try to read the question!
He wants to get rid of the www (or any other subdomain) from the host and show only the domain.
December 13, 2011
$w_url[]=weburl.split['/'];
February 1, 2012
function getDomain()
{
preg_match(“#^.*?([a-z0-9-]+\.[a-z]{0,3})$#i”, $_SERVER['HTTP_HOST'], $matches);
return $matches[1];
}
February 16, 2012
Here is what i used to get the domain after reading the above:
// parse url to compnents e.g.: scheme=> http; host=> autos.yahoo.co.uk; path=> /new-cars.html
$parse = parse_url($url);
// Extract domain name from host compnent.
$url_domain_dot_ar = explode(“.”, $parse[host]);
$url_domain = $url_domain_dot_ar[1] . ‘.’ . $url_domain_dot_ar[2];
if ($url_domain_dot_ar[2] != “com”) { $url_domain = $url_domain . ‘.’ . $url_domain_dot_ar[3];}
echo ‘Domain from dot array parsed is : ‘ . $url_domain . “”;
It does account for subdomain.domain.com and for domain.co.uk.
April 21, 2012
the second one is not working domains such mkj.co.in india.ind.in
etc
April 21, 2012
Got better solution It will solve your problems..
; // outputs 'india.dj'
<php
print get_domain("http://whois.india.dj"
function get_domain($url)
{
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i’, $domain, $regs)) {
return $regs['domain'];
}
return false;
?>
May 7, 2012
how to create own domain