关于urlencode:url的编码之家-urlQueryEscapeencodeURIComponenturlencodeRFC3986

RFC3986首先,理解一下 RFC 3986 规范,简略讲就是规定了如下:除了 数字 + 字母 + -_.~ 不会被本义,其余字符都会被以百分号(%)后跟两位十六进制数 %{hex} 的形式进行本义。 再者,理解下 www 的 post form data 也就是 x-www-form-urlencode 的编码规定:除 -_.(没有 ~) 之外的所有 非字母、非数字 的字符都将被替换成 百分号(%)后跟两位十六进制数 %{hex},空格(留神)则编码为加号 +。 二者的区别如下:1、rfc3986 对 ~ 不做转码,x-www-form-urlencode 对 ~ 做转码 %7E。2、rfc3986 对 空格 转为 %20,x-www-form-urlencode 对 空格 转为 +。 接下来看几个高级语言的 url 编码方式。 js encodeURIComponentphp urlencode/rawurlencodego url.QueryEscapejsencodeURIComponentconsole.log(encodeURIComponent("hello233 ~-_."))hello233%20~-_.能够看到 js 齐全遵循 rfc3986,保留了 ~-_.,空格 被转码为 %20,正规。 phpurlencode<?phpecho urlencode("hello233 ~-_.");hello233+%7E-_.空格 转 +,只保留 -_. 没保留 ~,典型的 x-www-form-urlencode 规定。 ...

September 17, 2022 · 2 min · jiezi

关于urlencode:Javascript-URL-encoding

URL编码,有时也称为百分比编码,是一种将URL中的任何数据编码为能够在internet上传输的平安格局的机制。URL编码还用于为提交具备application/x-www-form-urlencoded MIME类型的HTML表单筹备数据。encodeURIComponent()留神,不应该应用encodeURIComponent()函数对整个URL进行编码。它应该只用于对查问字符串或门路段进行编码: var baseUrl = 'https://www.google.com/search?q='var query = 'Hellö Wörld@Javascript'// encode only the query stringvar completeUrl = baseUrl + encodeURIComponent(query)console.log(completeUrl)// https://www.google.com/search?q=Hell%C3%B6%20W%C3%B6rld%40JavascriptencodeURI()语法:encodeURI(URI) 参数URI:A complete URI. const uri = 'https://mozilla.org/?x=';const encoded = encodeURI(uri);console.log(encoded);// expected output: "https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B"encodeURI vs encodeURIComponentconst set1 = ";,/?:@&=+$#"; // Reserved Charactersconst set2 = "-_.!~*'()"; // Unreserved Marksconst set3 = "ABC abc 123"; // Alphanumeric Characters + Spaceconsole.log(encodeURI(set1)); // ;,/?:@&=+$#console.log(encodeURI(set2)); // -_.!~*'()console.log(encodeURI(set3)); // ABC%20abc%20123 (the space gets encoded as %20)console.log(encodeURIComponent(set1)); // %3B%2C%2F%3F%3A%40%26%3D%2B%24%23console.log(encodeURIComponent(set2)); // -_.!~*'()console.log(encodeURIComponent(set3)); // ABC%20abc%20123 (the space gets encoded as %20)URLEncoder (在线) ...

June 13, 2022 · 1 min · jiezi