从完整域中删除域名[重复]

This question already has an answer here:

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!

</div>

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

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

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