在PHP中解码字符串时出现意外行为(来自AJAX POST调用)
I have some javascript that sends data in JSON through POST format to a PHP script.
Everything works fine with "usual" characters, but I see inconsistencies when using, for example, vowels with accents such as "à". I would like to ask if anyone has suggestions on how to fix this.
This is the Javascript:
$.ajax({
contentType: 'application/json',
data: JSON.stringify({
"action": params.action,
"username": params.username,
"page": params.page,
}),
processData: false,
//dataType: 'json',
url: "/w/ImolaCustom/SudoAutoedit.php",
type: 'POST',
success: function(data) {
...
}
});
On the PHP side of things I do this:
$theData = json_decode(file_get_contents('php://input')), true);
The problem presents itself if I send something like:
params.page = "Società sportiva Bridge";
as $theData['page'] becomes "Societ\xc3\xa0 sportiva Bridge"
If I use utf8_decode($theData['page']) (or if I use it on the string passed from php://input before json_decoding it I get "Societ\xe0 sportiva Bridge" instead.
I tried different conversion functions like iconv(), mb_convert_variables() and mb_convert_encoding() to convert from UTF-8 to ISO-8859-1 with the same results as above.
I also tried encoding the string client-side with encodeURIComponent() or escape(). PHP receives the correct string (respectively "Societ%C3%A0%20sportiva%20Bridge" and "Societ%E0%20sportiva%20Bridge"), but after decoding it with rawurldecode() I still get "Societ\xc3\xa0 sportiva Bridge" and "Societ\xe0 sportiva Bridge" respectively.
Both files are on a CentOS machine and are saved with EOL Conversion in UNIX Mode and with Charset Encoding set to UTF-8 (editor is notepad++).
我有一些javascript通过POST格式将数据以JSON格式发送到PHP脚本。 p> \ n
“常用”字符一切正常,但是在使用带有重音符号的元音(例如“à”)时,我会看到不一致。 我想问一下是否有人就如何解决此问题提出了建议。 p>
这是Javascript: p>
$ .ajax({
contentType:'application / json',
data:JSON.stringify({
“action”:params.action,
“username”:params.username,
“page”:params.page,\ n}),
processData:false,
// dataType:'json',
url:“/ w / ImolaCustom / SudoAutoedit.php”,
n type:'POST',
success:function(data ){
...
}
});
code> pre>
在PHP方面,我这样做: p>
$ theData = json_decode(file_get_contents('php:// input')),true);
code> pre>
如果我发送问题,问题就出现了 类似于: p>
params.page =“SocietàsportivaBridge”;
code> pre>
as the the data ['page ']成为“Societ \ xc3 \ xa0 sportiva Bridge” p>
如果我使用utf8_decode($ theData ['page'])(或者如果我在从php传递的字符串上使用它:/ /在j之前输入 son_decoding它我改为“Societ \ xe0 sportiva Bridge”。 p>
我尝试了不同的转换函数,如iconv(),mb_convert_variables()和mb_convert_encoding(),以便从UTF-8转换为ISO- 8859-1与上面的结果相同。 p>
我也尝试使用encodeURIComponent()或escape()对字符串客户端进行编码。 PHP收到正确的字符串(分别为“Societ%C3%A0%20sportiva%20Bridge”和“Societ%E0%20sportiva%20Bridge”),但在用rawurldecode()解码之后我仍然得到“Societ \ xc3 \ xa0 sportiva Bridge” 两个文件都在CentOS机器上,并在UNIX模式下与EOL Conversion一起保存,并且Charset Encoding设置为UTF-8(编辑器是notepad ++)和“Societ \ xe0 sportiva Bridge”。 p>
div>
Please try this:
$content = file_get_contents('php://input');
$content = mb_convert_encoding($content, 'UTF-8',
mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
$theData = json_decode($content, true);
OR:
$content = file_get_contents('php://input');
$content = html_entity_decode(mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8"));
$theData = json_decode($content, true);
I hope this will help you.