PHP: convert spaces in string into %20?

独自空忆成欢 提交于 2020-01-11 15:17:08

问题


How can I convert spaces in string into %20?

Here is my attempt:

$str = "What happens here?";
echo urlencode($str);

The output is "What+happens+here%3F", so the spaces are not represented as %20.

What am I doing wrong?


回答1:


Use the rawurlencode function instead.




回答2:


The plus sign is the historic encoding for a space character in URL parameters, as documented in the help for the urlencode() function.

That same page contains the answer you need - use rawurlencode() instead to get RFC 3986 compatible encoding.




回答3:


I believe that, if you need to use the %20 variant, you could perhaps use rawurlencode().



来源:https://stackoverflow.com/questions/5572718/php-convert-spaces-in-string-into-20

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!