tags:

views:

3499

answers:

6

I need to build a function which parses the domain from a url.

So:

http://google.com/dhasjkdas/sadsdds/sdda/sdads.html or http://www.google.com/dhasjkdas/sadsdds/sdda/sdads.html

Would become "google.com" and

http://google.co.uk/dhasjkdas/sadsdds/sdda/sdads.html

would become "google.co.uk"

+2  A: 

Check out parse_url()

Greg
+12  A: 

check out parse_url():

$url = 'http://google.com/dhasjkdas/sadsdds/sdda/sdads.html';
$parse = parse_url($url);
print $parse['host']; // prints 'google.com'

note: parse_url doesn't handle really badly mangled urls very well, but is fine if you generally expect decent urls.

Owen
One thing parse_url() does not do is only return the domain. If you add www.google.com or www.google.co.uk, it will return the host as well. Any suggestions for that?
Crad
+1  A: 

From http://us3.php.net/manual/en/function.parse-url.php#93983

for some odd reason, parse_url returns the host (ex. example.com) as the path when no scheme is provided in the input url. So I've written a quick function to get the real host:

function getHost($Address) { 
   $parseUrl = parse_url(trim($Address)); 
   return trim($parseUrl['host'] ? $parseUrl['host'] : array_shift(explode('/', $parseUrl['path'], 2))); 
} 

getHost("example.com"); // Gives example.com 
getHost("http://example.com"); // Gives example.com 
getHost("www.example.com"); // Gives www.example.com 
getHost("http://example.com/xyz"); // Gives example.com 
philfreo
Don’t forget to quote your strings like `host` and `path`.
Gumbo
just copied it as-is from the php.net comment, but done now
philfreo
+2  A: 
$domain = str_ireplace('www.', '', parse_url($url, PHP_URL_HOST));

This would return the google.com for both http://google.com/... and http://www.google.com/...

Alix Axel
A: 
$url = 'http://google.com/dhasjkdas/sadsdds/sdda/sdads.html';
$parse = parse_url($url);
print $parse['host']; // prints 'google.com'

if we check subdomain just liek www.games.google.com

its show : games.google.com

how can i handlel that thing?

muhammad
A: 

The internal PHP function "parse_url" is not always sufficient to parse URLs or URIs correctly into their components, such as Host/Domain, Path, Segments, Query and Fragment.

In this case you need a reliable result array like this:

  • [SCHEME] => http
  • [AUTHORITY] => user:[email protected]:80
  • [USERINFO] => user:pass
  • [USER] => user
  • [PASS] => pass
  • [HOST] => www.domain.com
  • [REGNAME] => www.domain.com
  • [DOMAIN] => www.domain.com
  • [LABEL][] =>
  • [LABEL][] => com
  • [LABEL][] => domain
  • [LABEL][] => www
  • [PORT] => 80
  • [PATH] => /dir1/dir2/page.html
  • [SEGMENT][] => dir1
  • [SEGMENT][] => dir2
  • [SEGMENT][] => page.html
  • [QUERY] => key1=value1&key2=value2
  • [GET][key1] => value1
  • [GET][key2] => value2
  • [FRAGMENT] => anchor/line
  • [ANCHOR][] => anchor
  • [ANCHOR][] => line

There's a standard-compliant, robust and performant PHP Class for handling and parsing URLs / URIs according to RFC 3986 and RFC 3987 available for download and free use:

http://andreas-hahn.com/en/parse-url

Andreas M. Hahn