Use the PHP parse_url() function.
This function returns an associative array containing any of the various components of the URL that are present. If one of them is missing, no entry will be created for it. The components are :
scheme - e.g. http
host
port
user
pass
path
query - after the question mark ?
fragment - after the hashmark #
$url = parse_url("http://username:password@hostname/path?arg=value#anchor");
will return
$url['scheme'] = http
$url['host'] = hostname
$url['user'] = username
$url['pass'] = password
$url['path'] = /path
$url['query'] = arg=value
$url['fragment'] = anchor