Definition and Usage
The rawurlencode()
function is used to safely encode individual URI components—such as search terms, filenames, and query parameter values—according to the URI specification defined in RFC 3986, rather than the entire URL.
Basic Example
$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'
Features
- The
rawurlencode()
function encodes characters with special meaning in URLs—such as protocol, domain, and path delimiters (/
,:
,?
,&
,#
)—according to the URI specification defined in RFC 3986. - Since it encodes most special characters, this function is useful when you need to safely encode individual URI components, such as search terms, filenames, or query parameter values.
- This function encodes characters like
/
,:
,?
,&
, and=
, which play a structural role in a URL. For this reason, it is not suitable for encoding an entire URL. These characters are essential for defining a URL's syntax—such as separating protocols, paths, queries, and parameters. If all of them are encoded, the resulting string will no longer be interpreted as a valid URL by a browser.
However, if the URL is being stored as a plain string or used only for transmission (e.g., in data payloads), fully encoding it using this function can be safe and appropriate.
Important!
Keep in mind that the space character is encoded as %20
by the rawurlencode()
function. In contrast, the urlencode()
function encodes spaces as +
instead.
Therefore, if you need to encode spaces as +
—for example, when adding form data to a query string or passing it to another page—it's better to use the urlencode()
function instead of rawurlencode()
.
Syntax
rawurlencode(string $string): string
Parameters
$string |
The string to be encoded as part of a URL. |
---|
Return Values
All characters except uppercase and lowercase letters, digits, and the characters -
, _
, .
, and ~
are replaced with a percent sign (%
) followed by two hexadecimal digits. Spaces are encoded as %20
.
This encoding, described in RFC 3986, prevents literal characters from being interpreted as special URL delimiters and protects URLs from corruption by transmission media that may alter characters (such as some email systems).
Practical Examples
The rawurlencode()
function is primarily used when strict adherence to URL syntax rules or specific requirements is necessary. In most other cases, the urlencode()
function is sufficient.
One common scenario where rawurlencode()
is preferred is when adding file paths to a URL. File paths may contain spaces or special characters that need to be correctly encoded to form a valid URL.
For example, consider a case where you need to append an image file path to a web server URL. The file path may include spaces and special characters such as slashes (/
).
$server_url = 'https://www.example.com';
$image_path = '/images/my image.jpg';
// Use rawurlencode() when appending a file path to a URL
$encoded_image_path = rawurlencode($image_path);
$image_url = $server_url . $encoded_image_path;
echo $image_url;
// Output: https://www.example.com%2Fimages%2Fmy%20image.jpg
In the example above, spaces and slashes in the file path are safely encoded using rawurlencode()
before appending to the URL. This ensures that special characters in the file path comply with URL syntax rules and are correctly processed by the web.
Thus, rawurlencode()
is useful when dealing with data containing spaces or special characters—such as file paths—that need to strictly conform to URL rules.
References
See also
- PHP URL Encoding Functions
- PHP urlencode() Function – Encoding a URL Query Parameter Value
- 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 rawurldecode() Function – Decoding URLs Encoded with rawurlencode()
- PHP parse_str() Function – Parse URL Query Strings into Variables