urlencode
Description
Syntax
urlencode(
string $string
): stringParameters
string
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 · not replaced
<? $string = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz". "-_."; $return = urlencode($string); echo $return;
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.
2 · replaced · %
<?
$string = "`!@#$%^&*()+={[}]|\:;\"'<,>?/".
"~";
$return = urlencode($string);
echo $return;
%60%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%7E
3 · replaced · +
<? $string = " "; $return = urlencode($string); echo $return;
+
4 · url
<? $string = "url to be encoded"; $return = urlencode($string); echo '<a href="https://osbo.com?arg=' . $return . '">' . $string . '</a>';
<a href="https://osbo.com?arg=url+to+be+encoded">url to be encoded</a>