PHP json_encode json_decode UTF-8

前端 未结 10 850
温柔的废话
温柔的废话 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:26

    I had the same problem. It might differ depending on how You put the data to the db, but try what worked for me:

    $str = json_encode($data);
    $str = addslashes($str);
    

    Do this before saving data to db.

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

    Work for me :)

    function jsonEncodeArray( $array ){
        array_walk_recursive( $array, function(&$item) { 
           $item = utf8_encode( $item ); 
        });
        return json_encode( $array );
    }
    
    0 讨论(0)
  • 2020-12-01 08:39

    If your source-file is already utf8 then drop the utf8_* functions. php5 is storing strings as array of byte.

    you should add a meta tag for encoding within the html AND you should add an http header which sets the transferencoding to utf-8.

    <html>
    <head>
       <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    

    and in php

    <?php
    header('Content-Type: text/html; charset=utf-8');
    
    0 讨论(0)
  • 2020-12-01 08:44

    This is an encoding issue. It looks like at some point, the data gets represented as ISO-8859-1.

    Every part of your process needs to be UTF-8 encoded.

    • The database connection

    • The database tables

    • Your PHP file (if you are using special characters inside that file as shown in your example above)

    • The content-type headers that you output

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