urlencode() 来自 PHP 中的 JavaScript?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7013643/
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-10-25 22:41:54  来源:igfitidea点击:

urlencode() from PHP in JavaScript?

phpjavascripturlurlencode

提问by daGrevis

I'm looking for similar function as urlencode()from PHP just in JavaScript. jQuery library is allowed.

我正在寻找与urlencode()PHP类似的功能,只是在 JavaScript 中。允许使用 jQuery 库。

Basically, I need to encode the string and then redirect the user to another page just with JavaScript.

基本上,我需要对字符串进行编码,然后仅使用 JavaScript 将用户重定向到另一个页面。

回答by Chronial

There is no function quite matching urlencode(), but there is one quite equivalent to rawurlencode(): encodeURIComponent().

没有完全匹配的函数urlencode(),但有一个完全等同于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

回答by Floern

Have a look at phpjs.org if you're searching for a JS function equivalent to PHP:

如果您正在搜索与 PHP 等效的 JS 函数,请查看 phpjs.org:

http://phpjs.org/functions/urlencode:573

http://phpjs.org/functions/urlencode:573

Here you can use encodeURIComponent()(with some modifications).

在这里你可以使用encodeURIComponent()(有一些修改)。

回答by nxy

My code is the JS function equivalent to PHP's urlencode (based on PHP's source code).

我的代码是相当于PHP的urlencode的JS函数(基于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;
}

回答by lpandzic

Try urlencode from http://locutus.io/php/url/urlencode/. I've tested it in several cases I had and it worked.

http://locutus.io/php/url/urlencode/尝试 urlencode 。我已经在几种情况下测试过它并且它有效。

回答by mickmackusa

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. This differs from the ? RFC 3986 encoding (see rawurlencode()) in that for historical reasons, spaces are encoded as plus (+) signs

返回一个字符串,其中除 -_ 之外的所有非字母数字字符。已替换为百分号 (%) 后跟两个十六进制数字和编码为加号 (+) 符号的空格。它的编码方式与来自 WWW 表单的发布数据的编码方式相同,即与 application/x-www-form-urlencoded 媒体类型中的方式相同。这与 ? 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:
Not Escaped: A-Z a-z 0-9 - _ . ! ~ * ' ( )

encodeURIComponent() 转义所有字符,除了:
Not Escaped: 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);
  });
}
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:

编码输出:

%2e_%21_%7e_%2a_%27_%28_%29-%5C-%26-%7C