Using PHP rawurldecode() to Decode URL‑Encoded Strings
This article explains the PHP rawurldecode() function, its syntax, how it decodes URL‑encoded strings produced by urlencode(), provides a code example, shows the output, and notes the difference from urldecode() for handling other characters.
In web development, URLs often contain special characters that must be encoded for proper transmission and later decoded for correct parsing. PHP offers a set of functions for handling URL encoding and decoding, among which rawurldecode() is used to decode URL‑encoded strings.
The rawurldecode() function takes a string that has been encoded with urlencode() and returns the original, decoded URL. It is particularly useful when you need to reverse the encoding applied by urlencode() .
Syntax of the function is:
string rawurldecode ( string $str )Here $str is the URL‑encoded string to be decoded, and the function returns the decoded result.
Below is a practical example demonstrating how to use rawurldecode() :
<?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 that has been encoded by urlencode() is assigned to $url . The rawurldecode() function decodes it, and the result is printed with echo .
Running the script produces the following output:
https://www.example.com/?q=добрый汉字The output shows that the encoded URL has been successfully restored to its original form.
It is important to note that rawurldecode() only decodes the special characters in the URL; it does not process other characters that may appear in query parameters. If you need to decode an entire URL, including plus signs and spaces, you should use urldecode() instead.
In summary, rawurldecode() is a convenient PHP function for decoding URLs that were encoded with urlencode() , helping ensure that URL parameters are correctly interpreted during web 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.