json_encode JSON_UNESCAPED_SLASHES无法正常工作,并且仍在转义斜线 [英] json_encode JSON_UNESCAPED_SLASHES not working and still escaping slashes

查看:465
本文介绍了json_encode JSON_UNESCAPED_SLASHES无法正常工作,并且仍在转义斜线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的自动完成搜索功能被破坏,因为带有重音符号的字符存储在mySQL中。

My autocomplete search feature is broken because of how characters with accents are stored in the mySQL.

例如,在mySQL列中,É的存储方式类似于\ u00c9

For example, in the mySQL column, É is stored like \u00c9

在接收用户输入并调用mySQL的PHP​​中,É是\xc3\x89

In the PHP, which receives the user's input and calls on mySQL, É is \xc3\x89

json_encode()几乎可以完美地将 \xc3\x89转换为 \u00c9

json_encode() almost works perfectly to take "\xc3\x89" and convert it to "\u00c9"

$clean = json_encode($criteria, JSON_UNESCAPED_SLASHES);

除非将其转换为 \\u00c9,所以字符甚至都不匹配

Except it converts it to "\\u00c9" and so the characters don't match even though they are both É.

选项JSON_UNESCAPED_SLASHES不起作用。为什么不能不将另一个反斜杠添加到反斜杠前面?

The option JSON_UNESCAPED_SLASHES isn't working. Why does it not keep another backslash from being added in front of the backslash?

我如何使它起作用?

编辑:我刚刚在下面添加了实际的代码和错误日志输出。代码:

I just added the actual code and error log output below. code:

error_log("criteria vvvvvvvvvvvvv");
error_log($criteria);

$clean = json_encode($criteria, JSON_UNESCAPED_SLASHES);  

error_log("json_encode(criteria) vvvvvvvvvvvvvv");
error_log($clean);

错误日志:

[Fri Aug 23] criteria vvvvvvvvvvvvvvv, 
[Fri Aug 23 \xc3\x89
[Fri Aug 23] json_encode(criteria) vvvvvvvvvvvvvvv, 
[Fri Aug 23] "\\u00c9"


推荐答案

第一个 JSON_UNESCAPED_SLASHES 用于防止转义顾名思义, SLASHES / 不要指望它会阻止 back 斜线 \

First JSON_UNESCAPED_SLASHES is used to prevent escaping "SLASHES" / as the name implies, don't expect it to prevent escaping backslashes \

echo json_encode('/'); // prints "\/"
echo json_encode('/', JSON_UNESCAPED_SLASHES); // prints "/"
echo json_encode("\\", JSON_UNESCAPED_SLASHES); // prints "\\"
//note on line 3 : the input is 1 backslash

如您所见,它只能防止转义斜杠,而不是反斜杠

As you can see it prevents escaping slashes only , not backslashes

关于您的问题,如果最终使用 json_encode 与类似 \\u00c9 的东西一样,那么您必须将此字符串作为输入 \u00c9 json_encode()没做错什么,用字符串 \u00c9而不是Unicode字符 00c9 并且它在字符串开头转义了反斜杠。

Regarding your problem, if you ended up by using json_encode with something like \\u00c9 then you must have gave it this string as input \u00c9 , json_encode() did nothing wrong , you feed it with the string "\u00c9" not the Unicode character00c9 and it escaped the backslash at the string beginning.

您的 $ criteria 变量可能包含JSON编码的字符串,例如 \u00c9 已在不使用 JSON_UNESCAPED_UNICODE 选项,换句话说,不要两次使用 json_encode()

Your $criteria variable is probably holding a JSON encoded string like "\u00c9" that has been encoded without using the JSON_UNESCAPED_UNICODE option, in other words don't use json_encode() twice.

检查这些示例,可能会清除

check these examples, it could clear things out



echo json_encode("É", JSON_UNESCAPED_SLASHES) . "\n";
echo json_encode("\u00c9", JSON_UNESCAPED_SLASHES) . "\n";
echo json_encode("\xc3\x89", JSON_UNESCAPED_SLASHES) . "\n";
echo json_encode("/") . "\n";
echo json_encode("/", JSON_UNESCAPED_SLASHES) . "\n";
echo json_encode("\\", JSON_UNESCAPED_SLASHES) . "\n";

此输出

"\u00c9"
"\\u00c9"
"\u00c9"
"\/"
"/"
"\\"

实时演示

这篇关于json_encode JSON_UNESCAPED_SLASHES无法正常工作,并且仍在转义斜线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆