如何将字符串中的空格转换为 %20?
%20
下面是我的尝试:
$str = "What happens here?"; echo urlencode($str);
输出为 "What+happens+here%3F",因此空格不表示为 %20。
"What+happens+here%3F"
我做错了什么?
The plus sign is the historic encoding for a space character in URL parameters, as documented in the help for the urlencode() function.
urlencode()
That same page contains the answer you need - use rawurlencode() instead to get RFC 3986 compatible encoding.
rawurlencode()
Use the rawurlencode function instead.
rawurlencode
I believe that, if you need to use the %20 variant, you could perhaps use rawurlencode().