何时将空格编码为加号(+)或%20? [英] When to encode space to plus (+) or %20?

查看:307
本文介绍了何时将空格编码为加号(+)或%20?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时空格将URL编码为+符号,有时将其编码为%20.有什么区别,为什么要发生?

Sometimes the spaces get URL encoded to the + sign, some other times to %20. What is the difference and why should this happen?

推荐答案

+表示application/x-www-form-urlencoded内容中仅 的空格,例如URL的查询部分:

+ means a space only in application/x-www-form-urlencoded content, such as the query part of a URL:

http://www.example.com/path/foo+bar/path?query+name=query+value

在此URL中,参数名称为带空格的query name,值是带空格的query value,但路径中的文件夹名称实际上为foo+bar不是 foo bar.

In this URL, the parameter name is query name with a space and the value is query value with a space, but the folder name in the path is literally foo+bar, not foo bar.

%20是在这些上下文中的任何一种下对空格进行编码的有效方法.因此,如果您需要对字符串进行URL编码以包含在URL的一部分中,则始终可以安全地用%20替换空格,并用%2B替换加号.这就是例如. encodeURIComponent()在JavaScript中起作用.不幸的是,这不是 urlencode 在PHP中所做的(

%20 is a valid way to encode a space in either of these contexts. So if you need to URL-encode a string for inclusion in part of a URL, it is always safe to replace spaces with %20 and pluses with %2B. This is what eg. encodeURIComponent() does in JavaScript. Unfortunately it's not what urlencode does in PHP (rawurlencode is safer).

另请参阅 HTML 4.01规范应用程序/x-www-form-urlencoded

这篇关于何时将空格编码为加号(+)或%20?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆