note 93983 modified in function.parse-url by danbrown
| From: | danbrown@php.net | Date: | Fri, 09 Oct 2009 21:52:24 +0000 |
| Subject: | note 93983 modified in function.parse-url by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-161609@lists.php.net to get a copy of this message | ||
Hello, 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:
<?php
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
?>
You could try anything! It gives the host (including the subdomain if exists).
Hope it helped you.
--was--
Hello, for some odd reason, parse_url returns the host (ex. google.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:
<?php
function getHost($Address) {
$parseUrl = parse_url(trim($Address));
return trim($parseUrl[host] ? $parseUrl[host] : array_shift(explode('/',
$parseUrl[path], 2)));
}
getHost("google.com"); // Gives google.com
getHost("http://google.com"); // Gives google.com
getHost("www.google.com"); // Gives www.google.com
getHost("http://google.com/xyz"); // Gives
google.com
?>
You could try anything! It gives the host (including the subdomain if exists).
Hope it helped you.
http://php.net/manual/en/function.parse-url.php