rawurlencode

URL-encode according to RFC 3986

Syntax

rawurlencode ( string $str ) : string

Parameters

str

The URL to be encoded.

Return

Returns a string in which all non-alphanumeric characters except -_.~ have been replaced with a percent (%) sign followed by two hex digits. This is the encoding described in RFC 3986 for protecting literal characters from being interpreted as special URL delimiters, and for protecting URLs from being mangled by transmission media with character conversions (like some email systems).

Examples

1

<?

$str = "-_.~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

$return = rawurlencode($str);

echo $return;

?>
-_.~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

2

<?

$str = " !@#$%^&*()+={[}]|\:;\"'<,>?/";

$return = rawurlencode($str);

echo $return;

?>
%20%21%40%23%24%25%5E%26%2A%28%29%2B%3D%7B%5B%7D%5D%7C%5C%3A%3B%22%27%3C%2C%3E%3F%2F

3

<?

$str = "encode non-alphanumeric";

$return = rawurlencode($str);

echo '<a href="https://domain.com/path?var=' . $return . '">' . $str . '</a>';

?>
<a href="https://domain.com/path?var=encode%20non-alphanumeric">encode non-alphanumeric</a>
HomeMenu