【问题标题】:Javascript encodeURIComponent doesn't encode single quotesJavascript encodeURIComponent 不编码单引号
【发布时间】:2021-12-11 02:00:09
【问题描述】:

试试看:

encodeURIComponent("'@#$%^&");

如果你试试这个,你会看到除了单引号之外的所有特殊字符都被编码了。我可以使用什么函数对所有字符进行编码并使用 PHP 对其进行解码?

谢谢。

【问题讨论】:

  • 您能否更具体地了解 PHP 部分?你怎么寄给他们?什么目前不起作用?
  • 整个过程解释起来有点复杂。我使用 ajax 和 PHP 将编码值发送到数据库(这就是为什么必须对引号进行编码,否则会出现 MySQL 问题)然后我使用 PHP 检索该值并使用 rawurldecode() 对其进行解码
  • 啊,不!出于安全原因,PHP必须自行转义所有 SQL 查询。如果引号需要被 JS 转义,那你就做错了。

标签: javascript


【解决方案1】:

我不确定您为什么要对它们进行编码。如果只想转义单引号,可以使用.replace(/'/g, "%27")。然而,好的参考是:

【讨论】:

  • 首先我不想使用这样的东西,但显然 PHP 的 rawurldecode 支持解码来自“%27”的引用。所以,问题解决了!干杯伙伴。
  • 关于你为什么要这样做的问题:如果你想使用 javascript 来构建 html,例如,var html = "<a href='" + link + "'>foo</a>";,你需要确保内部单引号被正确转义.
  • @speedplane 但是你不使用encodeURI(Component)。如果您构建 HTML 字符串,则使用 htmlAttributeEscape!
  • @Bergi htmlAttributeEscape 是标准函数吗?
  • @speedplane 不,我的意思是any function that escapes HTML 属性值
【解决方案2】:

你可以使用:

function fixedEncodeURIComponent (str) {
  return encodeURIComponent(str).replace(/[!'()*]/g, escape);
}

fixedEncodeURIComponent("'@#$%^&");

查看参考:http://mdn.beonex.com/en/JavaScript/Reference/Global_Objects/encodeURIComponent.html

【讨论】:

  • 是否有通用的方法来结合 escape 和 encodeURIComponent?.. escape 可以使用单引号,但 fixedEncodeURIComponent 可以使用 '#'..
【解决方案3】:

您可以使用btoa()atob(),这将编码和解码给定的字符串,包括单引号。

【讨论】:

    【解决方案4】:

    自己试试encodeURI()encodeURIComponent()...

    console.log(encodeURIComponent('@#$%^&*'));

    输入:@#$%^&*。输出:%40%23%24%25%5E%26*。那么,等等,* 发生了什么事?为什么没有转换? TLDR:您实际上想要fixedEncodeURIComponent()fixedEncodeURI()。长篇大论...

    encodeURIComponent()不要使用。使用fixedEncodeURIComponent(),正如MDN encodeURIComponent() Documentation所定义和解释的那样,强调我的...

    为了更严格地遵守 RFC 3986(保留 !、'、(、) 和 *),即使这些字符没有正式的 URI 分隔符用途,以下可以安全使用:

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

    当我们谈到这个话题时,也不要使用encodeURI()。如MDN encodeURI() Documentation 所定义,MDN 也有自己的重写。引用他们的解释...

    如果希望遵循更新的 RFC3986 的 URL,它保留方括号(用于 IPv6),因此在形成可能是 URL 一部分的内容(例如主机)时不编码,则以下代码 sn- p 可能会有所帮助:

    function fixedEncodeURI(str) { return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']'); }

    【讨论】:

      【解决方案5】:

      我发现了一个不会漏掉任何字符的巧妙技巧。我告诉它替换一切,除了什么都没有。我这样做(URL编码):

      function encode(w){return w.replace(/[^]/g,function(w){return '%'+w.charCodeAt(0).toString(16)})}
      

      function encode(w){return w.replace(/[^]/g,function(w){return '%'+w.charCodeAt(0).toString(16)})}
      
      loader.value = encode(document.body.innerHTML);
      <textarea id=loader rows=11 cols=55>www.WHAK.com</textarea>

      【讨论】:

        【解决方案6】:

        正如@Bergi 所写,您可以替换所有字符:

        function encoePicture(pictureUrl)
        {
         var map=
         {
                  '&': '%26',
                  '<': '%3c',
                  '>': '%3e',
                  '"': '%22',
                  "'": '%27'
         };
        
         var encodedPic = encodeURI(pictureUrl);
         var result = encodedPic.replace(/[&<>"']/g, function(m) { return map[m];});
         return result;
        }
        

        【讨论】:

          【解决方案7】:

          最近的回答(2021 年)

          使用 JavaScript 的 URLSearchParams:

          console.log(new URLSearchParams({ encoded: "'@#$%^&amp;" }).toString())

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-10-16
            • 2013-09-21
            • 1970-01-01
            • 1970-01-01
            • 2021-06-17
            • 2022-01-05
            • 1970-01-01
            • 2015-01-19
            相关资源
            最近更新 更多