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.

php Courses
php Courses
php Courses
Using PHP's urldecode() Function to Decode URLs

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendURLweb-developmenturldecode
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.