PHP: How to Break a URL

How do I get URL elements and any associated parameters?

Two cases may appear: either the URL is in the character string and you want to find its elements, or this is a web script launched to process form data using the POST method, from which you want to extract data.

1) Using the parse_url function

When parsing a URL in a string, two functions come in handy: parse_url and parse_str. The first splits the URL into elements such as Location in JavaScript, the second splits the parameter string into variables and values .

Comparison with JavaScript

The function parse_url to the JavaScript equivalent of a Location object. Associative table keys are replaced by the properties of the Location object.

The complete URL is obtained with the __ constant FILE __ which is global and does not belong to the table created by the parse_url.

Data
PHP
JavaScript
Full URL
__ FILE __
location .href
Protocol (http)
structure
protocol
Area
host
hostname
Directory and file
way
pathname
Binding to page
fragment
hash
Parameters
query
search
Port
-
port
Login
to use
-
Passcode
admission
-
PHP equivalence table - JavaScript

Other differences:

Using the parse_url Feature

Example:

$url = "https://www.iqlevsha.ru/comment/parser-url.php#content";
$arr = parse_url($url)
print_r($arr);

The result is an associative table, where the keys are described in detail in the previous table.

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

Using the parse_str Feature

Example:

$parameters = $arr["query"];

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

$ data is an associative array, where the keys are the variables passed to the parameters, and the values, the contents of these variables.

Exempli gratia:

fichier.php?nom=untel&numero=50

The generated table will be:

array(
"nom" => untel,
"numero" => 50
)

Sources and loading

2) Using the $ _ SERVER variable

The second part shows how to get the same information using the $ _ SERVER preset variable and, in particular, get the form data to switch to PHP script.

Comparison with parse_url

Information about the PHP script or page is obtained using the $ _ SERVER preset variable and a list of keys corresponding to the found information, which are shown in the table below.

Let's clarify that the domain name is assigned directly by the user. Using our example, this is www.example.com.

Data
parse_url
Keys for $ _ SERVER
Full URL
__ FILE __
domain + REQUEST_URI
Protocol
structure
SERVER_PROTOCOL
Area
host
user constant
Directory and file
way
SCRIPT_NAME
Binding to page
fragment
-
Parameters
query
QUERY_STRING
Port
-
-
Login
to use
Treated separately
Passcode
admission
Treated separately
Equivalence Table parse_url to $ _ SERVER

Differences:

Using a preset variable

Example:

$protocol = $_SERVER[SERVER_PROTOCOL];

The list of keys is presented in the PHP guide in the section: "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 the keys are the variables passed to the parameters, and the values, the contents of these variables.

For details, see the first part of the article.

Sources and loading