This function parses a URL and returns an associative array containing any of the various components of the URL that are present. It is important to note that parse_url does not validate the given URL, it simply breaks it up into parts. Partial URLs are also accepted, and whilst parse_url tries its best to parse them correctly, it may return -1 if the URL provided is too malformed.
So what exactly does it break an URL up into then?
Well, given an URL like below, parse_url would return the following:
$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
So pretty useful if you want say just the host or query value from a given URL then!
Another thing to note is that it is not designed to work with relative URLs, meaning that you do need to provide it with fully qualified URLs in order to get something usable back.
Still, a pretty nifty little function to keep in mind when writing parsing scripts…
Related Link: http://www.php.net/manual/en/function.parse-url.php