Definition and Usage
The rawurldecode()
function decodes percent-encoded characters represented by a percent sign (%
) followed by two hexadecimal digits back to their original characters, and converts %20
into a space character.
This function is primarily used to decode URI components—such as path segments or query parameter values—that have been rawurlencode()
-encoded in a strict manner.
Basic Example
/* Encode a query parameter value using rawurlencode() */
$keyword = 'iPhone & Galaxy/Note=100% #1';
$encoded = rawurlencode($keyword);
echo 'https://example.com/search?query=' . $encoded;
// Output: 'https://example.com/search?query=iPhone%20%26%20Galaxy%2FNote%3D100%25%20%231'
/* Decode the query parameter using rawurldecode() */
$decoded = rawurldecode($encoded);
echo $decoded;
// Output: 'iPhone & Galaxy/Note=100% #1'
Features
- Decodes a URL-encoded string back to its original form.
- Optimized for decoding URLs encoded with the
rawurlencode()
function. - Converts
%20
into a space character.
The rawurlencode()
function encodes a given string for use in a URL according to RFC 3986.
RFC 3986 is the standard for URL encoding, where all characters except uppercase and lowercase letters, digits, -
, _
, .
, and ~
are replaced with a percent sign (%
) followed by two hexadecimal digits. Spaces are encoded as %20
.
Syntax
rawurldecode(string $string): string
Parameters
$string |
The encoded URL string to decode. |
---|
Return Values
Decodes all %##
encoding in the given $string
, converting %20
sequences into space characters. Returns the decoded string.
Practical Examples
// Server URL and image path containing spaces and special characters
$server_url = 'https://www.example.com';
$image_path = '/images/my image.jpg';
// Use rawurlencode() when appending file paths to URLs
$encoded_image_path = rawurlencode($image_path);
$image_url = $server_url . $encoded_image_path;
echo $image_url . '<br>';
// Output: 'https://www.example.com%2Fimages%2Fmy%20image.jpg'
$rawurlencode_image_path = rawurldecode($image_path);
echo $rawurlencode_image_path;
// Output: '/images/my image.jpg'
File paths may contain spaces, slashes (/
), and other special characters.
In the example above, the rawurlencode()
function is used to safely append the file path—including spaces and slashes—to the URL, ensuring that special characters conform to URL encoding rules and are correctly handled by web browsers.
Strings encoded with rawurlencode()
can be safely decoded back to their original form using the rawurldecode()
function.
References
See also
- PHP URL Encoding Functions
- PHP urlencode() Function – Encoding a URL Query Parameter Value
- PHP rawurlencode() Function – Safely Encoding URI Components
- PHP http_build_query() Function – Generate URL-Encoded Query Strings from Arrays or Objects
- PHP URL Decoding Functions
- PHP urldecode() Function – Decoding URLs Encoded with urlencode()
- PHP parse_str() Function – Parse URL Query Strings into Variables