Function name: urlencode()
Applicable versions: All PHP versions
Usage: The urlencode() function is used to URL encode a string and convert special characters into % symbols followed by two-digit hexadecimal numbers. This allows you to pass data to the URL safely. Generally used to build URL parameters.
Syntax: string urlencode ( string $str )
parameter:
Return value: Returns the URL encoded string.
Example:
$str = "Hello, world!"; $encodedStr = urlencode($str); echo $encodedStr; // 输出:Hello%2C+world%21 $url = "https://www.example.com/search?q=" . urlencode("sunny day"); echo $url; // 输出:https://www.example.com/search?q=sunny+day
In the example, we first URL encode the string "Hello, world!" and get "Hello%2C+world%21". Note that the spaces are encoded as the "+" symbol.
Then, we use the urlencode() function to URL encode the search term "sunny day" and splice it into the URL, and we get the complete search URL " https://www.example.com/search?q=sunny+day".
By using the urlencode() function, we ensure that special characters in the URL are correctly encoded for easy passing to the server or access in the browser.