【问题标题】:Different results between JS encodeURI() and PHP urldecode()JS encodeURI() 和 PHP urldecode() 结果不同
【发布时间】:2011-11-07 18:15:02
【问题描述】:

这是原始字符串

Apple iPad 2 16GB Wifi + 3G (AT&T)

js后encodeURI()

Apple%20iPad%202%2016GB%20Wifi%20+%203G%20(AT&T)

在 PHP urldecode() 之后,通过 AJAX 调用中的 $_GET

Apple iPad 2 16GB Wifi   3G (AT&T)

插入 MySQL DB 后用单引号括起来

Apple iPad 2 16GB Wifi   3G (AT

如何将原始字符串准确输入数据库?

【问题讨论】:

  • 如果数据在插入 MySQL 之前是有效的,则问题与 URL 编码无关。另外,您如何传递从 PHP 中 decode 所需的 GET 参数?解码是自动的。

标签: php javascript database url


【解决方案1】:

您应该使用 encodeURIComponent 而不是 encodeURI。

那么字符串在 JavaScript 中 encodeURIComponent 之后就是 Apple%20iPad%202%2016GB%20Wifi%20%2B%203G%20(AT%26T),在 PHP 中会通过 urldecode 转换回 Apple iPad 2 16GB Wifi + 3G (AT&T)

【讨论】:

    【解决方案2】:

    您的数据库字段很可能不够长,无法包含完整的字符串。

    您还应该使用encodeURIComponent 而不是encodeURI 对字符串进行编码

    【讨论】:

      【解决方案3】:

      查找PHP.JS's urlencode function - 它的行为方式与 PHP 的 urlencode 完全相同,因此 urldecode 将完美运行。

      【讨论】:

        【解决方案4】:

        PHP 中的rawurlencode 将保留“+”。不确定与符号,您可以尝试将字符串传递给 htmlentities。

        【讨论】:

          猜你喜欢
          • 2016-03-18
          • 2012-04-27
          • 2018-02-11
          • 1970-01-01
          • 1970-01-01
          • 2011-06-23
          • 1970-01-01
          • 2013-08-10
          • 2012-07-24
          相关资源
          最近更新 更多