[英]urlencode() from PHP in JavaScript?
I'm looking for similar function as urlencode()
from PHP just in JavaScript.我正在从 JavaScript 中的 PHP 中寻找类似的 function 作为
urlencode()
。 jQuery library is allowed.允许使用 jQuery 库。
Basically, I need to encode the string and then redirect the user to another page just with JavaScript.基本上,我需要对字符串进行编码,然后使用 JavaScript 将用户重定向到另一个页面。
There is no function quite matching urlencode()
, but there is one quite equivalent to rawurlencode()
: encodeURIComponent()
.没有与
urlencode()
完全匹配的 function ,但有一个与rawurlencode()
完全等效: encodeURIComponent()
。
Usage: var encoded = encodeURIComponent(str);
用法:
var encoded = encodeURIComponent(str);
You can find a reference here:您可以在此处找到参考:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent
From: https://www.php.net/manual/en/function.urlencode.php来自: https://www.php.net/manual/en/function.urlencode.php
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.
它的编码方式与 WWW 表单中发布的数据的编码方式相同,即与 application/x-www-form-urlencoded 媒体类型中的方式相同。 This differs from the » RFC 3986 encoding (see rawurlencode()) in that for historical reasons, spaces are encoded as plus (+) signs
这与 » RFC 3986 编码(参见 rawurlencode())的不同之处在于,由于历史原因,空格被编码为加号 (+)
From: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent :来自: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent :
encodeURIComponent() escapes all characters except:
encodeURIComponent() 转义所有字符,除了:
Not Escaped: AZ az 0-9 - _.未转义:AZ az 0-9 - _。 !
! ~ * ' ( )
~ * ' ( )
A snippet is provided near the bottom of that page which looks like this:该页面底部附近提供了一个片段,如下所示:
function fixedEncodeURIComponent(str) { return encodeURIComponent(str).replace(/[,'()*]/g. function(c) { return '%' + c.charCodeAt(0);toString(16); }); }
I am slightly adjusting the provided javascript snippet to include a couple more characters.我正在稍微调整提供的 javascript 片段以包含更多字符。
My Code:我的代码:
function urlEncodeLikePHP(str) {
return encodeURIComponent(str).replace(/[.!~*'()]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16);
});
}
Usage:用法:
urlEncodeLikePHP("._!_~_*_'_(_)-\\-&-|-/");
// effectively: "._!_~_*_'_(_)-\-&-|-/"
Encoded Output:编码 Output:
%2e_%21_%7e_%2a_%27_%28_%29-%5C-%26-%7C
Have a look at phpjs.org if you're searching for a JS function equivalent to PHP:如果您正在搜索等效于 PHP 的 JS function,请查看 phpjs.org:
http://phpjs.org/functions/urlencode:573 http://phpjs.org/functions/urlencode:573
Here you can use encodeURIComponent()
(with some modifications).在这里您可以使用
encodeURIComponent()
(进行一些修改)。
My code is the JS function equivalent to PHP's urlencode (based on PHP's source code).我的代码是 JS function 等效于 PHP 的 urlencode(基于 PHP 的源代码)。
function urlencode(str) {
let newStr = '';
const len = str.length;
for (let i = 0; i < len; i++) {
let c = str.charAt(i);
let code = str.charCodeAt(i);
// Spaces
if (c === ' ') {
newStr += '+';
}
// Non-alphanumeric characters except "-", "_", and "."
else if ((code < 48 && code !== 45 && code !== 46) ||
(code < 65 && code > 57) ||
(code > 90 && code < 97 && code !== 95) ||
(code > 122)) {
newStr += '%' + code.toString(16);
}
// Alphanumeric characters
else {
newStr += c;
}
}
return newStr;
}
Try urlencode from http://locutus.io/php/url/urlencode/ .从http://locutus.io/php/url/urlencode/尝试 urlencode。 I've tested it in several cases I had and it worked.
我已经在我的几个案例中测试过它并且它有效。
encodeURIComponent()编码URIComponent()
http://www.w3schools.com/jsref/jsref_encodeURIComponent.asp http://www.w3schools.com/jsref/jsref_encodeURIComponent.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.