Using PHP's urldecode Function to Decode URL Parameters and Full URLs
This article explains how to use PHP's urldecode function to decode URL-encoded parameters and full URLs, provides practical code examples, demonstrates the relationship with urlencode, and highlights best practices for handling special characters in web development.
In web development we often need to handle URL parameters, and special characters in those parameters are URL‑encoded, so they must be decoded. PHP provides the urldecode function for this purpose.
This article demonstrates how to use PHP’s urldecode function with practical code examples.
First, a simple example with an encoded URL parameter:
$url = "https://example.com/?name=%E5%BC%A0%E4%B8%89&age=20";In this URL the name parameter is encoded as %E5%BC%A0%E4%B8%89 . We can decode it using urldecode :
$name = urldecode($_GET['name']);
echo $name;Running the code outputs “张三”, showing that urldecode converts the encoded string back to readable Chinese characters.
We can also decode an entire URL. Example:
$url = "https%3A%2F%2Fexample.com%2F%3Fname%3D%E5%BC%A0%E4%B8%89%26age%3D20";
$decodedUrl = urldecode($url);
echo $decodedUrl;The result is https://example.com/?name=张三&age=20 , demonstrating that the function works on full URLs as well.
When a URL contains special characters, it is advisable to first encode it with urlencode and then decode it with urldecode to ensure correctness.
$name = "张三";
$encodedName = urlencode($name);
$decodedName = urldecode($encodedName);
echo $decodedName;This outputs “张三”, confirming that the round‑trip encoding and decoding preserves the original string.
In summary, PHP’s urldecode function provides a straightforward way to decode both individual URL parameters and whole URLs, making it a useful tool for handling URL‑related operations in backend development.
PHP实战开发极速入门
扫描二维码免费领取学习资料
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.