Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - URL rawurldecode() Function



The PHP URL rawurldecode() function is used to decode a URL encoded strings. It basically changes URL-encoded characters back to its original form.

This can return a string in which sequences with percent (%) signs followed by two hex digits have been replaced with literal characters.

Syntax

Below is the syntax of the PHP URL rawurldecode() function −

string rawurldecode(string $str)

Parameters

This function accepts $str parameter which is the URL-encoded string you want to decode.

Return Value

The rawurldecode() function returns the decoded string which is the decoded URL.

PHP Version

First introduced in core PHP 4, the rawurldecode() function continues to work easily in PHP 5, PHP 7, and PHP 8 versions.

Example 1

First we will show you the basic example of the PHP URL rawurldecode() function to get the decoded string of the given encoded string or URL.

<?php
   // Define the encoded string here
   $str = "Hello%20World%21";
   $decoded_str = rawurldecode($str);
   echo $decoded_str; 
?>

Output

The above code will result something like this −

Hello World!

Example 2

This example will use the rawurldecode() function and decode a string with special characters.

<?php
   /$encoded_str = "Amit%20Deshmukh%40example.com";
   $decoded_str = rawurldecode($encoded_str);
   echo $decoded_str; 
?> 

Output

After running the above program, it generates the following output −

AmitDeshmukh@example.com

Example 3

Now the below code retrieves decoded path of the given URL encoded path with the help of rawurldecode() function.

<?php
   // Mention encoded string here
   $encoded_path = "%2Fhome%2FPHP%2Fdocuments%2Ffile.txt";
   $decoded_path = rawurldecode($encoded_path);
   echo $decoded_path; 
?> 

Output

This will create the below output −

/home/PHP/documents/file.txt

Example 4

In the following example, we are using the rawurldecode() function to get the decoded URL-encoded query parameters.

<?php
   // Mention encoded string here
   $encoded_query = "name%3DAmit%26age%3D33";
   $decoded_query = rawurldecode($encoded_query);
   echo $decoded_query; 
?> 

Output

When the above program is executed, it will produce the below output −

name=Amit&age=33
php_function_reference.htm
Advertisements