PHP:将字符串中的空格转换为%20?

时间:2011-04-06 20:45:54

标签: php string urlencode

如何将字符串中的空格转换为%20

这是我的尝试:

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

输出为"What+happens+here%3F",因此空格不表示为%20

我做错了什么?

3 个答案:

答案 0 :(得分:176)

改为使用rawurlencode功能。

答案 1 :(得分:25)

加号是网址参数中空格字符的历史编码,urlencode()函数为documented in the help

同一页面包含您需要的答案 - 使用rawurlencode()来获得RFC 3986兼容编码。

答案 2 :(得分:19)

我相信,如果您需要使用%20变体,则可以使用rawurlencode()

相关问题