PHP json_encode json_decode UTF-8

前端 未结 10 849
温柔的废话
温柔的废话 2020-12-01 07:45

How can I save a json-encoded string with international characters to the databse and then parse the decoded string in the browser?



        
相关标签:
10条回答
  • 2020-12-01 08:17
      header('Content-Type: application/json; charset=utf-8');
    
    0 讨论(0)
  • 2020-12-01 08:19
    1. utf8_decode $j_decoded = utf8_decode(json_decode($j_encoded)); EDIT or to be more correct $j_encoded = json_encode($j_encoded); $j_decoded = json_decode($j_encoded); no need for en/decoding utf8
    2. <meta charset="utf-8" />
    0 讨论(0)
  • 2020-12-01 08:21

    json utf8 encode and decode:

    json_encode($data, JSON_UNESCAPED_UNICODE)
    
    json_decode($json, false, 512, JSON_UNESCAPED_UNICODE)
    

    force utf8 might be helpfull too: http://pastebin.com/2XKqYU49

    0 讨论(0)
  • 2020-12-01 08:21

    Try sending the UTF-8 Charset header:

    <?php header ('Content-type: text/html; charset=utf-8'); ?>
    

    And the HTML meta:

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    
    0 讨论(0)
  • 2020-12-01 08:21

    For me both methods

    <?php
    
    header('Content-Type: text/html; charset=utf-8');
    
    echo json_encode($YourData, \JSON_UNESCAPED_UNICODE);
    
    0 讨论(0)
  • 2020-12-01 08:26

    if you get "unexpected Character" error you should check if there is a BOM (Byte Order Marker saved into your utf-8 json. You can either remove the first character or save if without BOM.

    0 讨论(0)
提交回复
热议问题