Using PHP rawurldecode() to Decode URL‑Encoded Strings
This article explains PHP's rawurldecode() function, its syntax, usage examples for decoding URL‑encoded strings, differences from urldecode(), and practical considerations for handling special characters in web development, including code demonstrations and expected output.
In web development, we often need to handle URLs, and special characters in URLs must be encoded to be correctly transmitted and parsed. In some cases we need to decode URLs, restoring the encoded string to the original URL. PHP provides a set of functions to handle URL encoding and decoding, one of which is the rawurldecode() function.
The rawurldecode() function decodes a URL‑encoded string back to the original URL. It is mainly used to decode strings encoded by the urlencode() function for processing and display.
Below is the syntax of the rawurldecode() function:
string rawurldecode ( string $str )Here, $str represents the URL string to be decoded, and the function returns the decoded string.
Below is a code example showing how to use the rawurldecode() function for URL decoding:
<?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 by urlencode()
$decodedUrl = rawurldecode($url);
echo $decodedUrl;
?>In the above example, a URL is encoded with urlencode() and assigned to $url , then rawurldecode() decodes it. Finally, the echo statement outputs the decoded URL string.
Running the code produces the following output:
https://www.example.com/?q=добрый汉字We can see that after decoding the encoded URL string with rawurldecode() , we obtain the original URL string.
Note that rawurldecode() only decodes special characters in the URL; it does not process other characters in URL parameters. To decode an entire URL, you can use the urldecode() function.
In summary, rawurldecode() is a convenient PHP function for decoding URLs. It decodes strings encoded by urlencode() back to the original URL. In web development it is often used to handle URL encoding and decoding, ensuring correct transmission and parsing of URL parameters.
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.