在服务器端,PHP代码将使用 urldecode() 函数进行解码,但JavaScript代码负责对URL进行编码 . 以下哪个JavaScript函数与PHP urldecode() 函数兼容:
urldecode()
escape()encodeURI()encodeURIComponent()
您可以使用 encodeURI 或 encodeURIComponent . php手册说明:
encodeURI
encodeURIComponent
解码给定字符串中的任何%##编码 .
因此无论编码函数编码如何,所有 %## 序列都被解码 . 因此,您可以使用其中一个JavaScript函数对其进行编码 .
%##
(对Gumbo的回答有何回应,他删除了哪些?)
php的 urldecode 也将 + 符号解码为空格(因为它实现了不同的标准) . 为了确保在php端没有解码实际意图的加号,只需使用 encodeURIComponent 即可 . 这将 + 编码为 %2B ,然后再从php的 urldecode 安全 .
urldecode
+
%2B
1 回答
您可以使用
encodeURI
或encodeURIComponent
. php手册说明:因此无论编码函数编码如何,所有
%##
序列都被解码 . 因此,您可以使用其中一个JavaScript函数对其进行编码 .编辑:
(对Gumbo的回答有何回应,他删除了哪些?)
php的
urldecode
也将+
符号解码为空格(因为它实现了不同的标准) . 为了确保在php端没有解码实际意图的加号,只需使用encodeURIComponent
即可 . 这将+
编码为%2B
,然后再从php的urldecode
安全 .