【问题标题】:urlencode() from PHP in JavaScript?JavaScript 中来自 PHP 的 urlencode()?
【发布时间】:2011-10-24 05:26:27
【问题描述】:

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

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

【问题讨论】:

    标签: php javascript url urlencode


    【解决方案1】:

    没有完全匹配urlencode()的函数,但是有一个完全等价于rawurlencode()的函数:encodeURIComponent()

    用法:var encoded = encodeURIComponent(str);

    你可以在这里找到参考:

    https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        【解决方案3】:

        发件人:https://www.php.net/manual/en/function.urlencode.php

        返回一个字符串,其中包含除 -_ 之外的所有非字母数字字符。 已替换为百分号 (%) 符号后跟两个十六进制数字 和编码为加号 (+) 的空格。它的编码方式与 来自 WWW 表单的发布数据被编码,这与在 application/x-www-form-urlencoded 媒体类型。这不同于 » 由于历史原因,RFC 3986 编码(参见 rawurlencode()), 空格被编码为加号(+)

        发件人:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

        encodeURIComponent() 转义所有字符,除了:
        未转义:A-Z a-z 0-9 - _ 。 ! ~ * ' ( )

        在该页面底部附近提供了一个 sn-p,如下所示:

        function fixedEncodeURIComponent(str) {
          return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
            return '%' + c.charCodeAt(0).toString(16);
          });
        }
        

        我正在稍微调整提供的 javascript sn-p 以包含更多字符。

        我的代码:

        function urlEncodeLikePHP(str) {
            return encodeURIComponent(str).replace(/[.!~*'()]/g, function(c) {
                return '%' + c.charCodeAt(0).toString(16);
            });
        }
        

        用法:

        urlEncodeLikePHP("._!_~_*_'_(_)-\\-&-|-/");
        // effectively: "._!_~_*_'_(_)-\-&-|-/"
        

        编码输出:

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

        【讨论】:

          【解决方案4】:

          我的代码是相当于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;
          }
          

          【讨论】:

            【解决方案5】:

            【讨论】:

            • 这不一样。一个例子是encodeURIComponent() 不编码括号。
            【解决方案6】:

            尝试来自 http://locutus.io/php/url/urlencode/ 的 urlencode。 我已经在几个案例中对其进行了测试,并且成功了。

            【讨论】:

              猜你喜欢
              • 2014-10-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多