Search notes:

PHP code snippets: the urldecode() function

urldecode() can be used to decode percent encoded characters of an URI.
The following example tries to demonstrate that the values in the super global associative array $_GET is already url-decoded, but not the the value of $_SERVER['QUERY_STRING'].
<?php
print('<html><head>
<meta charset="utf-8">
<title>urldecode</title></head><body>
');

printf('<form>
   <input type="text" name="words" value="%s" />
   <input type="submit"/>
</form>', htmlentities(
           array_key_exists('words', $_GET) ? $_GET ['words'] : "O'briƤn & company"
          )
);

print("<hr><table border=1 style='border-collapse:collapse'>");
printf("<tr><td>\$_GET['words']</td><td>%s</td></tr>"                     ,           htmlentities(array_key_exists('words'       , $_GET   ) ? $_GET   ['words'       ] : "") );
printf("<tr><td>\$_SERVER['QUERY_STRING']</td><td>%s</td></tr>"           ,           htmlentities(array_key_exists('QUERY_STRING', $_SERVER) ? $_SERVER['QUERY_STRING'] : "") );
printf("<tr><td>urldecode(\$_SERVER['QUERY_STRING'])</td><td>%s</td></tr>", urldecode(htmlentities(array_key_exists('QUERY_STRING', $_SERVER) ? $_SERVER['QUERY_STRING'] : "")));
print("</table>");

print("</body></html>");
?>
Github repository about-php, path: /urldecode.php

See also

htmlspecialchars()
Other PHP snippets.

Index