URL Decoding Functions Comparison
PHP provides three main functions for URL decoding:
These functions are used to decode encoded URL strings, each serving a different purpose and approach. This section explains the purpose and key features of each function.
Function Purpose & Features
The following table compares the primary uses of each function.
Comparison Item | urldecode() |
rawurldecode() |
parse_str() |
---|---|---|---|
Primary Use | This function is primarily used to restore URL query parameter values that have been encoded using the urlencode() function. |
This function is primarily used to decode URI components—such as path segments or query values—that have been strictly encoded using the rawurlencode() function. |
This function is used to decode a query string that has been generated using the http_build_query() function and parse it into individual PHP variables. |
urldecode()
The following example demonstrates how to use the urldecode()
function to restore URL query parameter values that were encoded and passed using the urlencode()
function.
// Encode a string for use in a URL using the urlencode() function
$url = 'https://www.example.com/page.php?name=John Doe';
$encoded_url = urlencode($url);
echo $encoded_url . '<br>';
// Output: 'https%3A%2F%2Fwww.example.com%2Fpage.php%3Fname%3DJohn+Doe'
// Decode a URL encoded with urlencode()
$decoded_url = urldecode($url);
echo $decoded_url;
// Output: 'https://www.example.com/page.php?name=John Doe'
Features
- Decodes a URL-encoded string back to its original form.
- Optimized for decoding URLs encoded with the
urlencode()
function. - Converts plus signs (
+
) into a space character.
The urlencode()
function encodes a given string for use in a URL by replacing spaces with plus signs (+
). The urldecode()
function decodes percent-encoded characters represented by a percent sign (%
) followed by two hexadecimal digits back to their actual characters, and converts plus signs (+
) into space characters.
rawurldecode()
The following example demonstrates how to use the rawurldecode()
function to decode URI components—such as path segments or query parameter values—that have been strictly encoded and passed using the rawurlencode()
function.
/* Encode a query parameter value using rawurlencode() */
$keyword = 'iPhone & Galaxy/Note=100% #1';
$encoded = rawurlencode($keyword);
echo 'https://example.com/search?query=' . $encoded;
// Output: 'https://example.com/search?query=iPhone%20%26%20Galaxy%2FNote%3D100%25%20%231'
/* Decode the query parameter using rawurldecode() */
$decoded = rawurldecode($encoded);
echo $decoded;
// Output: 'iPhone & Galaxy/Note=100% #1'
Features
- Decodes a URL-encoded string back to its original form.
- Optimized for decoding URLs encoded with the
rawurlencode()
function. - Converts
%20
into a space character.
The rawurlencode()
function encodes a given string for use in a URL according to RFC 3986.
RFC 3986 is the standard for URL encoding, where all characters except uppercase and lowercase letters, digits, -
, _
, .
, and ~
are replaced with a percent sign (%
) followed by two hexadecimal digits. Spaces are encoded as %20
.
parse_str()
The following example demonstrates how to use the parse_str()
function to decode a query string that has been encoded for use in a URL and parse it into variables.
// An array to be encoded into a URL-encoded query string
$data = array(
'name' => 'John Doe',
'age' => 30,
'city' => 'New York'
);
// Encode the array into a query string using http_build_query()
$queryString = http_build_query($data);
echo $queryString; // Output: name=John+Doe&age=30&city=New+York'
// Decode the query string using parse_str()
// Store the parsed data as an array in the second argument, $decodedArray
parse_str($queryString, $decodedArray);
print_r($decodedArray); // Output: Array ( [name] => John Doe [age] => 30 [city] => New York )
Features
- Instead of returning a value, this function stores the parsed data as an array in the second argument passed to it.
- The
parse_str()
function converts a URL-encoded query string generated byhttp_build_query()
into key-value pairs and assigns them to an array or variables.
http_build_query()
generates a URL-encoded query string from an array or object.
See also
- PHP URL Encoding Functions
- PHP urlencode() Function – Encoding a URL Query Parameter Value
- PHP rawurlencode() Function – Safely Encoding URI Components
- PHP http_build_query() Function – Generate URL-Encoded Query Strings from Arrays or Objects
- PHP urldecode() Function – Decoding URLs Encoded with urlencode()
- PHP rawurldecode() Function – Decoding URLs Encoded with rawurlencode()
- PHP parse_str() Function – Parse URL Query Strings into Variables