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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5572718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:50:23  来源:igfitidea点击:

PHP: convert spaces in string into %20?

phpstringurlencode

提问by matt

How can I convert spaces in string into %20?

如何将字符串中的空格转换为%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+happens+here%3F",因此空格未表示为%20

What am I doing wrong?

我究竟做错了什么?

回答by Matthew Flaschen

Use the rawurlencodefunction instead.

请改用该rawurlencode函数。

回答by Alnitak

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

加号是 URL 参数中空格字符的历史编码,如该函数的帮助中所述urlencode()

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

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

回答by David says reinstate Monica

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

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