php URL decode get '+' from URL

Ed R picture Ed R · Mar 31, 2011 · Viewed 12.4k times · Source

So I am trying to encode/decode a url that when decoded will return the encoded + symbols from teh url. For example, I encodewebsite.com/index.php?eq=1+12 which when encoded turns the + into %2B, as it should. When I retrieve the value from $_REQUEST['eq'] and use urldecode() it echo's as "1 12". I cannot seem to get the decode to bring back the + so to speak. Am I doing something wrong here, or is there a more efficient/better way to go about doing this? Here is the exact encode/decode lines I use.

Submit Page

<?php
$eq = "1+12";
$send = '<a href="website.com/index.php?eq='.urlencode($eq).'</a>';
echo $send;

Retrieval page

<?php
$eq = urldecode($_REQUEST['eq']);
echo $eq;
?>

Answer

Quentin picture Quentin · Mar 31, 2011

Don't run urldecode, the data in $_REQUEST is automatically decoded for you.

A plus sign, in a URL, is an encoded space. PHP decodes the hex value to a + automatically. Then, by running the result through urldecode, you are manually (and incorrectly) decoding the + to a .