note 93983 added to function.parse-url
| From: | nirazuelos at gmail dot com | Date: | Fri, 09 Oct 2009 21:45:45 +0000 |
| Subject: | note 93983 added to function.parse-url | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-161608@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 79.177.101.36
----
Manual Page -- http://www.php.net/manual/en/function.parse-url.php
Edit -- https://master.php.net/note/edit/93983
Del: integrated -- https://master.php.net/note/delete/93983/integrated
Del: useless -- https://master.php.net/note/delete/93983/useless
Del: bad code -- https://master.php.net/note/delete/93983/bad+code
Del: spam -- https://master.php.net/note/delete/93983/spam
Del: non-english -- https://master.php.net/note/delete/93983/non-english
Del: in docs -- https://master.php.net/note/delete/93983/in+docs
Del: other reasons-- https://master.php.net/note/delete/93983
Reject -- https://master.php.net/note/reject/93983
Search -- https://master.php.net/manage/user-notes.php