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