Backend Development 4 min read

Using PHP rawurldecode() to Decode URL‑Encoded Strings

This article explains PHP's rawurldecode() function, detailing its purpose for decoding URL‑encoded strings, providing syntax, a step‑by‑step example that converts an encoded URL back to its original form, and notes its limitations compared to urldecode(), making it a useful tool for backend web development.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP rawurldecode() to Decode URL‑Encoded Strings

In web development we often need to handle URLs, and special characters must be encoded to be transmitted and parsed correctly. In some cases we need to decode a URL, restoring the encoded string to its original form. PHP provides a set of functions for URL encoding and decoding, one of which is rawurldecode() .

The rawurldecode() function decodes a URL‑encoded string back to its original URL. It is mainly used to reverse strings encoded with the urlencode() function, making them easier to process 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.

The following code example demonstrates how to use rawurldecode() 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 example, a URL is first encoded with urlencode() and assigned to the $url variable. The rawurldecode() function is then used to decode it, and the result is output with echo .

Running the code produces the following output:

https://www.example.com/?q=добрый汉字

This shows that after decoding the encoded URL string with rawurldecode() , we obtain the original URL.

Note that rawurldecode() only decodes special characters in the URL; it does not process other characters in URL parameters. If you need to decode an entire URL, you should use the urldecode() function instead.

In summary, rawurldecode() is a convenient PHP function for decoding URLs that have been encoded with urlencode() . It restores the original URL string and is frequently used in web development to handle URL encoding and decoding correctly.

backendweb developmentPHPrawurldecodeURL decoding
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.