urlencode

URL-encodes string

Syntax

urlencode ( string $str ) : string

Parameters

str

The string 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 and spaces encoded as plus (+) signs. It is encoded the same way that the posted data from a WWW form is encoded, that is the same way as in application/x-www-form-urlencoded media type. This differs from the RFC 3986 encoding (see rawurlencode()) in that for historical reasons, spaces are encoded as plus (+) signs.

Examples

1

<?

$str = "-_.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

$return = urlencode($str);

echo $return;

?>
-_.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

2

<?

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

$return = urlencode($str);

echo $return;

?>
%7E%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 = " ";

$return = urlencode($str);

echo $return;

?>
+

4

<?

$str = "encode non-alphanumeric";

$return = urlencode($str);

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

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