Using PHP rawurldecode() to Decode URL‑Encoded Strings
This article explains PHP's rawurldecode() function, detailing its syntax, how it reverses URL encoding performed by urlencode(), provides a complete code example, demonstrates the output, and highlights the difference between rawurldecode() and urldecode() for full URL decoding.
In web development we often need to handle URLs, and special characters must be encoded; sometimes we need to decode them. PHP provides a set of functions for URL encoding and decoding, one of which is rawurldecode() .
rawurldecode() decodes a URL‑encoded string back to its original form. It is mainly used to decode strings that were encoded with urlencode() .
The syntax of rawurldecode() is:
string rawurldecode ( string $str )$str represents the URL string to be decoded, and the function returns the decoded string.
Example usage:
<?php
$url = "https%3A%2F%2Fwww.example.com%2F%3Fq%3D%D0%B4%D0%BE%D0%B1%D1%80%D1%8B%D0%B9%E6%B1%89%E5%AD%97"; // URL encoded with urlencode()
$decodedUrl = rawurldecode($url);
echo $decodedUrl;
?>In this example, a URL is first encoded with urlencode() and stored in $url , then rawurldecode() restores the original URL, which is printed with echo .
Running the code outputs:
https://www.example.com/?q=добрый汉字Note that rawurldecode() only decodes special characters in the URL; it does not process other characters in query parameters. To decode an entire URL, use urldecode() instead.
In summary, rawurldecode() is a convenient PHP function for decoding URLs that have been encoded with urlencode() , helping ensure correct transmission and parsing of URL parameters in backend development.
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.