PHP: How to parse a URL

How to get the elements of a URL, and possibly associated parameters?

Two cases may arise: either the URL is in a string, and we want to find its parts, or it is a Web page and we want retrieve all the information about it or get data from a form.

1) Using the parse_url function

To parse a URL in a string, two functions will be useful, parse_url and parse_str. The first splits a URL in parts just as the Location object in JavaScript, the second splits a string of parameters into variables and values.

Comparison with JavaScript

The parse_url function is equivalent to the Location object in JavaScript. The keys of the associative array are replaced by the properties of the Location object.

The full URL is obtained with the constant __FILE__ which is predefined and does not belong to the table generated by parse_url.

Data
PHP
JavaScript
Full URL
__FILE__
location.href
Protocol (http)
scheme
protocol
Domain
host
hostname
Directory and file
path
pathname
Anchor in the page
fragment
hash
Parameters
query
search
Port
-
port
Login
user
-
Password
pass
-
Correspondence PHP - JavaScript

Other differences:

Using the parse_url function

Example:

$url = "http://www.scriptol.com/how-to/parsing-url.php#content";
$arr = parse_url($url)
print_r($arr);

This gives an associative array where the keys are detailed in the table above.

array(
"scheme" => http,
"host" => www.scriptol.com,
"path" => /comment/parser-url.php,
"fragment" => content
)

Using the parse_str function

Example:

$parameters = $arr["query"];
parse_str($parameters, $data);
print_r($data);

$data is an associative array, where key are variables given as parameters, and values, the content of these variables.

For example:

file.php?name=doe&number=50

This array will be generated:

array(
"name" => doe,
"number" => 50
)

Sources and download

2) Using the $_SERVER variable

This part shows how to get the same information, including obtaining data from a form to pass to a PHP script.

Comparison with parse_url

Data on the script or PHP page are obtained with the predefined variable $_SERVER and a list of keys corresponding to the information sought, which are given in the table below.

Take note that the domain name is given directly by the user. In our example, it is www.example.com.

Data
parse_url
Keys for $_SERVER
Full URL
__FILE__
domain + REQUEST_URI
Protocol
scheme
SERVER_PROTOCOL
Domain
host
user constant
Directory and file
path
SCRIPT_NAME
Anchor in the page
fragment
-
Parameters
query
QUERY_STRING
Port
-
-
Login
user
Treated separately
Password
pass
Treated separately
Correspondence between parse_url and $_SERVER

Differences:

Using the predefined variable

Example:

$protocol = $_SERVER[SERVER_PROTOCOL];

The list of keys is provided in the PHP manual under the heading: "predefined variables."

Retrieving data from an HTML form

Example:

$parameters = $_SERVER[QUERY_STRING];

parse_str($parameters, $data);
print_r($data);

$data is an associative array, where keys are variables given as parameters, and values, the content of these variables.

See the first part of the article for more details.

Sources and download