PHP 的 rawurlencode 不等于 JavaScript 的转义!为什么?
我意识到,当我在 PHP 中使用 urlencode 或 rawurlencode 编码简单字符 §(段落)时,我得到以下结果:“%C2%A7”。
但是当我在 Javascript 中使用转义来编码该字符时,我只得到“%A7”。
在这种情况下,我在运行 PHP 的服务器和尝试通过 ajax/jquery 获取数据的 javascript 客户端之间发送/接收数据时遇到编码问题。
我希望能够编写我想要的任何类型的文本。为此,我对文本进行编码并将其发送到后端 php 脚本,转义数据并发送。当我检索它时,在 php 端,我从 mysql 获取数据并执行 rawurlencode 并将其发送回来。
双方都工作在UTF-8模式下。 jquery ajax函数使用“contentType:application / x-www-form-urlencoded:charset = UTF-8”
调用,mysql服务器为客户端和服务器设置为UTF-8,并且php脚本开始回显 header( "application/x-www-form-urlencoded:charset=UTF-8");
为什么 PHP 会生成 %C2 东西,它会在 javascript 端生成字符。
有人可以帮忙吗?
i realized when i used urlencode or rawurlencode in PHP encoding the simple character § (paragraph) i get the following result: "%C2%A7".
But when i use escape in Javascript to encode that character, i get only "%A7".
In this case i have encoding problems when sending/receiving data between the server running PHP and the javascript client trying to fetch the data via ajax/jquery.
I want to be able to write any type of text i want. For this i encode the text and send it to the backend php script, escaping the data and sending. When i retrieve it, on php side i take the data from mysql and do rawurlencode and send it back.
Both sides, work in UTF-8 mode. jquery ajax function is called with "contentType: application/x-www-form-urlencoded:charset=UTF-8"
, mysql server is set for UTF-8 both for client and server, and the php script starts echoing with header( "application/x-www-form-urlencoded:charset=UTF-8");
Why is PHP producing that %C2 thing, which generates the character  on javascript side.
Coult somebody help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不久前遇到了同样的问题并找到了解决方案:
代码取自这里 - http://phpjs .org/functions/rawurlencode:501
希望有帮助。
I had the same problem a while ago and found the solution :
The code is taken from here - http://phpjs.org/functions/rawurlencode:501
Hope it helps.
这显然是一个字符集问题:(
终端显然不是在 utf8 模式下运行)
如果您的 PHP 代码中有文字
§
,请确保 php 文件 保存为 UTF8 。It's clearly a charset íssue:
(the terminal is obviously not running in utf8 mode)
If you have a literal
§
in your PHP code ensure that the php file is saved as UTF8.