3

I'm looking for a quick way of getting parts of a domain name:

Example: http://www.mydomain.com/something/hello.html

I need to get the "mydomain.com" part and nothing else.

But, the url may change sometime, for example:

http://mydomain.com/something/hello.html

http://www.mydomain.com/hello.html

http://mydomain.com

Any help would be great,

Thanks!

3 Answers3

2

You can use the parse_url to get information about an URL.

<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';

print_r(parse_url($url));

echo parse_url($url, PHP_URL_PATH);
?>

Array
(
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
)
/path
Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
1

You want parse_url. Pass it a url and it returns an associative array with values for:

  • scheme
  • host
  • port
  • user
  • pass
  • path
  • query
  • fragment
Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99
-2

string parse to first instance of '.com' and strip the rest of the domain name from string

M4V3R1CK
  • 765
  • 3
  • 9
  • 22
  • Not the best way of doing this - PHP has built in methods (`parse_url`) that are more efficient and are the *correct* way of achieving this. – Adam Hopkinson Sep 28 '12 at 08:14