PHP urldecode() Function
The PHP urldecode() function decodes any %## encoding in the given string. Plus symbols ('+') are decoded to a space character.
Syntax
urldecode(string)
Parameters
string |
Required. Specify the string to be decoded. |
Return Value
Returns the decoded string.
Example:
The example below shows the usage of urldecode() function.
<?php echo urldecode("https%3A%2F%2Fwww.alphacodingskills.com")."\n"; echo urldecode("https%3A%2F%2Fwww.alphacodingskills.com%2Fcompilers.php")."\n"; echo urldecode("https%3A%2F%2Fwww.alphacodingskills.com%2Fqt%2Fquicktables.php")."\n"; ?>
The output of the above code will be:
https://www.alphacodingskills.com https://www.alphacodingskills.com/compilers.php https://www.alphacodingskills.com/qt/quicktables.php
Example:
Consider the example below which shows how a string is encoded and decoded.
<?php $str1 = "https://www.alphacodingskills.com"; $encoded_str1 = urlencode($str1); echo "The string is: $str1 \n"; echo "Encoded string: $encoded_str1 \n"; $str2 = "https%3A%2F%2Fwww.alphacodingskills.com"; $decoded_str2 = urldecode($str2); echo "\nThe string is: $str2 \n"; echo "Decoded string: $decoded_str2 \n"; ?>
The output of the above code will be:
The string is: https://www.alphacodingskills.com Encoded string: https%3A%2F%2Fwww.alphacodingskills.com The string is: https%3A%2F%2Fwww.alphacodingskills.com Decoded string: https://www.alphacodingskills.com
Example:
Consider one more example which demonstrates on extracting data from encoded string using this function.
<?php $query = "my=apples&are=green+and+red"; foreach (explode('&', $query) as $chunk) { $param = explode("=", $chunk); if ($param) { printf("Value for parameter \"%s\" is \"%s\" \n", urldecode($param[0]), urldecode($param[1])); } } ?>
The output of the above code will be:
Value for parameter "my" is "apples" Value for parameter "are" is "green and red"
❮ PHP URLs Reference