Understanding PHP's urldecode() Function: Usage and Examples
This article explains PHP's urldecode() function, covering URL encoding concepts, the function's signature, practical encoding/decoding examples, limitations with non‑printable characters, and when to use rawurldecode() as an alternative.
In web application development, you often need to encode and decode URLs; PHP provides built‑in functions for this purpose, one of which is urldecode() . This article introduces the usage of urldecode() and provides example code.
First, understand the concepts of URL encoding and decoding. Certain characters (such as spaces, slashes, question marks) cannot appear directly in a URL and must be represented using a special encoding scheme; decoding reverses this process.
The urldecode() function is used to decode a URL‑encoded string. It takes a single parameter—the encoded string—and returns the original decoded string. The function prototype is:
string urldecode ( string $str )The function accepts only one argument, the string to be decoded, and returns the decoded result.
Below is a practical example. Suppose we have a URL parameter that needs decoding. First, we encode the parameter using urlencode() :
$param = "hello world";
$urlParam = urlencode($param);The resulting $urlParam value is "hello%20world". Next, we decode this encoded URL parameter using urldecode() :
$decodedParam = urldecode($urlParam);
echo $decodedParam;Running the above code outputs "hello world", demonstrating that urldecode() successfully converts the encoded string back to its original form.
Note that urldecode() cannot properly decode non‑printable characters in the range %00‑%20 or non‑ASCII characters below %7F. For such cases, the rawurldecode() function should be used.
In summary, urldecode() is a valuable PHP function for decoding URL‑encoded strings, allowing developers to easily revert encoded data to its original representation.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.