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:
- Using htaccess to redirect domain without http://
- Fix-> Warning: ereg_replace(): REG_BADRPT in PHP
- 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 store and retrieve checkbox value in php?
- Troubleshooting Common DNS Misconfiguration Errors
- how to change hostname in linux centos?
- Fix -> Caret/Cursor jumps to start of textarea in javascript
- Problem with Custom rewrite rule in WordPress


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