Mastering PHP’s urldecode: Decode URL Parameters and Full URLs Easily
This tutorial explains how to use PHP's urldecode function to decode both individual URL parameters and entire URLs, providing clear code examples and highlighting the importance of proper encoding and decoding for reliable web development.
In web development we often need to handle URL parameters, and special characters are URL‑encoded, so they must be decoded. PHP provides the urldecode function for this purpose.
This article shows how to use urldecode with practical code examples.
First, a simple example with an encoded query string:
$url = "https://example.com/?name=%E5%BC%A0%E4%B8%89&age=20";The name parameter is encoded as %E5%BC%A0%E4%B8%89. Decoding it:
$name = urldecode($_GET['name']);
echo $name;Running this code outputs 张三 , demonstrating that urldecode converts the encoded string back to readable Chinese characters.
Beyond decoding individual parameters, you can decode an entire URL:
$url = "https%3A%2F%2Fexample.com%2F%3Fname%3D%E5%BC%A0%E4%B8%89%26age%3D20";
$decodedUrl = urldecode($url);
echo $decodedUrl;This produces https://example.com/?name=张三&age=20 , showing that the whole URL becomes human‑readable.
Note that when a URL contains special characters, you should first encode it with urlencode and then decode it with urldecode to ensure correctness.
$name = "张三";
$encodedName = urlencode($name);
$decodedName = urldecode($encodedName);
echo $decodedName;The output is again 张三 , confirming that the encode‑then‑decode cycle restores the original string.
In summary, using PHP's urldecode to decode URLs is straightforward; whether decoding a single parameter or an entire URL, a single line of code suffices, making it a valuable tool for handling URL‑related operations.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
