0
Introduction
In web development, is very common to use links. It is also common that the links need to be manipulated to add or remove parameters. In this post, we will see how to manipulate a link and its parameters so easy and safe.
Breaking a URL in parts
When a programmer doesn’t know the capabilities of PHP to handle URLs, it is common venture to manipulate the string with the URL directly, making operations such as checking if there is ‘?’ in the URL and get the content to the left or right, etc..
The main function to “parse” a URL and break it into parts is parse_url. This function can be used for two purposes: to get all the parts of a URL or to obtain a specific part of a URL (for example, the protocol, the domain, the query string, etc.). For all parts, just pass the URL as a parameter and don’t enter the second parameter:
|
1 2 3 4 5 6 7 8 9 10 |
$parts = parse_url('http://scriptcase:password@test.com:80/a/b/c.php?x=1&y=2#item'); echo $parts['scheme'] . PHP_EOL; // Gets the protocol: "http" echo $parts['user'] . PHP_EOL; // Gets the user "rubens" echo $parts['pass'] . PHP_EOL; // Gets the password "senha" echo $parts['host'] . PHP_EOL; // Gets the domain "teste.com.br" echo $parts['port'] . PHP_EOL; // Gets the port "80" echo $parts['path'] . PHP_EOL; // Gets the path "a/b/c.php" echo $parts['query'] . PHP_EOL; // Gets the query string "x=1&y=2" echo $parts['fragment'] . PHP_EOL; // Gets the anchor (or fragment) "item" |
For a specific part, just pass the second parameter of the function, which specifies which part is desirable. This parameter can have one of the constants:
PHP_URL_SCHEME
PHP_URL_HOST
PHP_URL_PORT
PHP_URL_USER
PHP_URL_PASS
PHP_URL_PATH
PHP_URL_QUERY
PHP_URL_FRAGMENT
Note: If you want the file name, just apply the function on the basename of $parts['path'], as an example:
|
1 |
$file = isset($parts['path']) ? basename($parts['path']) : ''; |
Breaking the query string into parts
As you can see, the function parse_url returns the query string of the URL in the way it is and not divided into parts. To break the query string into parts, just use the function parse_str passing the query string as the first parameter and the second parameter as a vector. Vector will be populated with the variables present in the query string, so that the array index stores the variable name and each position points to the respective variable value (the value is automatically decoded urldecode). Here’s an example:
|
1 2 3 4 5 |
// Assuming $parts['query'] holds the value "x=1&y=2" parse_str($parts['query'],$queryArray); echo $queryArray['x'].PHP_EOL // Gets "1" echo $queryArray['y'].PHP_EOL // Gets "2" |
Modifying the URL or Query String
After breaking the URL and optionally the query string, just manipulate the vector $parts or $queryArray as desired. For example, let’s change the protocol from http to https, change the port from 80 to 81 and remove the parameter “x” and include the parameter “z” value “abc”:
|
1 2 3 4 5 6 7 8 9 10 11 |
// Changing the protocol to https $parts['schema'] = 'https'; // Changing the port to 81 $parts['port'] = 81; // Removing the variable "a" of the query array unset($queryArray['x']); // Including the variable "z" in the query array $queryArray['z'] = 'a b c'; |
Riding the URL with the parties
After manipulating the URL and/or query string, we now need to assemble the parts and form the URL as a string. To do this, simply use the function http_build_query to mount the query array to turn it into query string. This function now makes coding using urlencode, then you need not worry about reserved characters.
|
1 2 |
// Assemble the query string $parts['query'] = http_build_query($queryArray, null, '&'); |
Unfortunately, http_build_url function is only available via PECL. However, it is a very simple function to implement. See below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
/ ** * Mounts the URL for their parts. (as a function http_build_url) * @param array $parts Parties URL (like in return parse_url). * @Return string * / function build_url($parts){ $url =''; $url.=$parts['schema'].'://'; if(isset($parts['user'])){ $url.=urlencode($parts['user']); if(isset($parts['pass'])){ $url.=':'.urlencode($parts['pass']); } $url.='@'; } $url.=$parts['host']; if(isset($parts['port'])){ $url.=':'.number_format($parts['port']); } if(isset($parts['path'])){ $url.=$parts['path']; } if(isset($parts['query'])){ $url.='?'.$parts['query']; } if(isset($parts['fragment'])){ $url.='#'.urlencode($parts['fragment']); } return $url; } |
Now just use this function and run:
|
1 |
$url = build_url($parts); |





