Url 编码和 HTML 编码的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1812473/
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
Difference between Url Encode and HTML encode
提问by Quintin Par
What's the difference between an URL Encodeand a HTML Encode?
采纳答案by Mehrdad Afshari
HTML Encoding escapes special characters in strings used in HTML documents to prevent confusion with HTML elements like changing
HTML 编码会转义 HTML 文档中使用的字符串中的特殊字符,以防止与 HTML 元素混淆,例如更改
"<hello>world</hello>"
to
到
"<hello>world</hello>"
URL Encoding does a similar thing for string values in a URL like changing
URL 编码对 URL 中的字符串值执行类似的操作,例如更改
"hello+world = hello world"
to
到
"hello%2Bworld+%3D+hello+world"
回答by BenAlabaster
urlEncode replaces special characters with characters that can be understood by web browsers/web servers for the purpose of addressing... hence URL. For instance, spaces are replaced with %20, ' = %27 etc...
urlEncode 将特殊字符替换为网络浏览器/网络服务器可以理解的字符,以用于寻址......因此是 URL。例如,空格被替换为 %20、' = %27 等...
See these references:
请参阅这些参考资料:
- http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
- http://www.degraeve.com/reference/urlencoding.php
- http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
- http://www.degraeve.com/reference/urlencoding.php
HtmlEncode replaces special characters with character strings that are recognised by the HTML engine itself to render the content of the page - things like & becomes & or < = < > = <
this prevents the HTML engine from interpreting these characters as parts of the HTML markup and therefore render them as if they were strings.
HtmlEncode 用 HTML 引擎本身识别的字符串替换特殊字符以呈现页面内容 - 诸如 & 之类的东西会& or < = < > = <
阻止 HTML 引擎将这些字符解释为 HTML 标记的一部分,因此将它们呈现为字符串。
See this reference:
请参阅此参考:
回答by JaredPar
Both HTML and URL's are essentially very constrained languages. As a language they add meaning to specific keywords or operators. For both of these languages though, keywords are almost always single characters. For example
HTML 和 URL 本质上都是非常受限制的语言。作为一种语言,它们为特定的关键字或运算符添加了含义。但是,对于这两种语言,关键字几乎总是单个字符。例如
- HTML: > and <
- URL: / and :
- HTML:> 和 <
- 网址:/ 和:
In the use of each languagethough it is possible to use these constructs in a manner that does not ensure the meaning of the language. For instance this post contains a > character. I do not want it to be interpreted as HTML, just text.
在每种语言的使用中,虽然有可能以不确保语言含义的方式使用这些结构。例如,这篇文章包含一个 > 字符。我不希望它被解释为 HTML,只是文本。
This is where Encode and Decode methods come into play. These methods will respectively take a string and convert any of the characters that would otherwise be treated as keywords into an escaped form which will not be interpreted as part of the language.
这就是 Encode 和 Decode 方法发挥作用的地方。这些方法将分别采用一个字符串并将任何将被视为关键字的字符转换为不会被解释为语言的一部分的转义形式。
For instance: Passing > into HtmlEncode will return >
例如:将 > 传入 HtmlEncode 将返回 >
回答by Michael Bray
HTMLEncode and URLEncode deal with invalid characters in HTML and URLs, or more accurately, characters that need to be specially written to be interpreted correctly. For example, in HTML the < and > characters are used to indicate tags. Thus, if you wanted to write a math formula, something like 1+1 < 2+2, the '<' would normally be interpreted as the beginning of a tag. HTMLEncoding turns this character into "<" which is the encoded representation of the less-than sign. URLEncoding does the same, but for URLs, for which the special characters are different, although there is some overlap.
HTMLEncode 和 URLEncode 处理 HTML 和 URL 中的无效字符,或者更准确地说,需要专门编写才能正确解释的字符。例如,在 HTML 中,< 和 > 字符用于表示标签。因此,如果您想编写数学公式,例如 1+1 < 2+2,“<”通常会被解释为标签的开头。HTMLEncoding 把这个字符变成“<” 这是小于号的编码表示。URLEncoding 的作用相同,但对于特殊字符不同的 URL,尽管存在一些重叠。
回答by Pekka
I don't know what language you are working in, but the PHP manualfor example provides good explanations.
我不知道您使用的是哪种语言,但是例如PHP手册提供了很好的解释。
URLEncode
Returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs. It is encoded the same way that the posted data from a WWW form is encoded, that is the same way as in application/x-www-form-urlencoded media type. This differs from the ? RFC 1738 encoding (see rawurlencode()) in that for historical reasons, spaces are encoded as plus (+) signs.
网址编码
返回一个字符串,其中除 -_ 之外的所有非字母数字字符。已替换为百分号 (%) 后跟两个十六进制数字和编码为加号 (+) 符号的空格。它的编码方式与来自 WWW 表单的发布数据的编码方式相同,即与 application/x-www-form-urlencoded 媒体类型中的方式相同。这与 ? RFC 1738 编码(参见 rawurlencode()),因为历史原因,空格被编码为加号 (+)。