duangan6636 2016-07-27 13:42
浏览 98
已采纳

在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++).

  • 写回答

1条回答 默认 最新

  • donglun1918 2016-07-27 14:05
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?