Using PHP's urldecode() Function to Decode URLs
This article explains the concept of URL encoding and decoding, introduces PHP's urldecode() function with its prototype, provides step‑by‑step example code showing how to encode a string with urlencode() and then decode it back using urldecode(), and notes its limitations and the alternative rawurldecode() function.
When developing web applications, you often need to encode and decode URLs, and PHP offers built-in functions for this purpose, one of which is urldecode() . This article introduces the usage and example code of the urldecode() function.
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. It takes a single URL‑encoded string as its argument and returns the original string. Its prototype is:
string urldecode ( string $str )The function accepts only one parameter—the string to be decoded—and returns the decoded original string.
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". We then decode this encoded URL parameter with urldecode() :
$decodedParam = urldecode($urlParam);
echo $decodedParam;Running the code outputs "hello world", demonstrating that urldecode() successfully converts the encoded parameter back to its original string.
Note that urldecode() cannot properly decode non‑printable characters in the range %00 to %20 or non‑ASCII characters below %7F. For such cases, use the rawurldecode() function instead.
In summary, urldecode() is a useful PHP function for decoding URL‑encoded strings, allowing developers to easily revert encoded data to its original form.
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.