How do I parse a URL in PHP? [duplicate]

Solution 1:

You can use parse_url to get down to the hostname.

e.g.:

$url = "http://localhost/path/to/?here=there";
$data = parse_url($url);
var_dump($data);

/*
Array
(
    [scheme] => http
    [host] => localhost
    [path] => /path/to/
    [query] => here=there
)
*/

Solution 2:

$url = 'http://localhost/something/';
$parsedurl  = parse_url($url);
echo $parsedurl['scheme'].'://'.$parsedurl['host'];