• PHP Video Tutorials

PHP - urlencode() Function



urlencode() function is a URL-encodes string.

Syntax

string urlencode( string $str )

urlencode() function is convenient when an encoding string to be used in a query part of the URL, as a convenient way to pass variables to the next page.

urlencode() function can return a string in which all non-alphanumeric characters except "-_." are replaced with 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. It differs from RFC 3986 encoding for historical reasons, and spaces are encoded as plus (+) signs.

Example

<?php
   echo '<a href="mycgi?foo=', urlencode($userinput), '">';
?>

Example

<?php
   $query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
   echo '<a href="mycgi?' . htmlentities($query_string) . '">';
?>
php_function_reference.htm
Advertisements