Definition and Usage
The urldecode()
function decodes percent-encoded characters represented by a percent sign (%
) followed by two hexadecimal digits back to their original characters, and converts plus signs (+
) into space characters.
This function is primarily used to restore URL query parameter values that have been encoded using the urlencode()
function.
Basic Example
// 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.
Important Note
URLs obtained from Superglobals such as $_GET
and $_REQUEST
are already decoded by PHP. Using urldecode()
on these variables may cause unexpected results and pose security risks. Therefore, you should avoid decoding these variables again.
The urldecode()
function should not be used on inputs automatically decoded by PHP, like $_GET
and $_REQUEST
. Instead, it should only be applied when you need to decode URL strings that have been encoded manually.
Syntax
urldecode(string $string): string
Parameters
$string |
The encoded URL string to decode. |
---|
Return Values
Decodes all %##
encoding in the given $string
and converts plus signs (+
) into space characters. Returns the decoded string.
Practical Examples
In most cases, URL query parameter values encoded with urlencode()
and received in PHP via $_GET
or the parse_str()
function are automatically decoded. Therefore, you rarely need to use urldecode()
directly.
However, you should use urldecode()
explicitly in the following situations:
- When receiving the entire URL as a string and manually parsing the query string
- When encoded URL strings are stored externally, such as in APIs, logs, or databases
When receiving the entire URL as a string and manually parsing the query string
// Original string
$search = 'hello world!';
// Encode with urlencode()
$encodedSearch = urlencode($search);
// Build the URL
$url = 'https://example.com/search.php?q=' . $encodedSearch;
echo "Generated URL: $url";
// Output: 'Generated URL: https://example.com/search.php?q=hello+world%21'
// Extract query string from URL
$parsedUrl = parse_url($url);
$queryString = $parsedUrl['query']; // 'q=hello+world%21'
// Manually extract value from query string (without using parse_str)
$queryParts = explode('=', $queryString);
$encodedValue = $queryParts[1]; // hello+world%21
// Explicitly decode using urldecode()
$decodedValue = urldecode($encodedValue);
echo "Decoded search term (using urldecode): " . $decodedValue;
// Output: 'Decoded search term (using urldecode): hello world!'
When encoded URL strings are stored externally (e.g., in APIs, logs, databases)
// Encoded query string retrieved from a database or logs (includes English + encoded spaces only)
$encodedQuery = 'name=John+Doe&city=New+York';
// Split each query parameter pair
$pairs = explode('&', $encodedQuery);
$params = [];
foreach ($pairs as $pair) {
$parts = explode('=', $pair);
$key = $parts[0];
$value = isset($parts[1]) ? $parts[1] : '';
// Decode each value explicitly using urldecode()
$params[$key] = urldecode($value);
}
echo 'Name: ' . $params['name'] . '<br>'; // Output: 'Name: John Doe'
echo 'City: ' . $params['city'] . '<br>'; // Output: 'City: New York'
References
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 URL Decoding Functions
- PHP rawurldecode() Function – Decoding URLs Encoded with rawurlencode()
- PHP parse_str() Function – Parse URL Query Strings into Variables