简体   繁体   English

在PHP中解码字符串时发生意外行为(来自AJAX POST调用)

[英]Unexpected behaviour while decoding strings in PHP (from AJAX POST call)

I have some javascript that sends data in JSON through POST format to a PHP script. 我有一些JavaScript,可通过POST格式以JSON格式将数据发送到PHP脚本。

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: 这是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: 在PHP方面,我这样做:

$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" $ theData ['page']变为“ 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. 如果我使用utf8_decode($ theData ['page'])(或者如果在json_decoding之前在php:// input传递的字符串上使用它,则会得到“ Societ \\ xe0 sportiva Bridge”。

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. 我尝试了不同的转换函数,例如iconv(),mb_convert_variables()和mb_convert_encoding(),从UTF-8转换为ISO-8859-1,其结果与上述相同。

I also tried encoding the string client-side with encodeURIComponent() or escape(). 我也尝试使用encodeURIComponent()或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. PHP接收正确的字符串(分别为“ Societ%C3%A0%20sportiva%20Bridge”和“ Societ%E0%20sportiva%20Bridge”),但是在用rawurldecode()对其进行解码后,我仍然得到“ Societ \\ xc3 \\ xa0 sportiva Bridge”和“ Societ \\ xe0 sportiva Bridge”。

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++). 这两个文件都在CentOS机器上,并以UNIX模式下的EOL Conversion和Charset Encoding设置为UTF-8(编辑器为notepad ++)保存。

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. 我希望这能帮到您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM