PHP json_encode json_decode UTF-8

178,247

Solution 1

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

Solution 2

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

Solution 3

  header('Content-Type: application/json; charset=utf-8');

Solution 4

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');

Solution 5

  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" />
Share:
178,247
FFish
Author by

FFish

Updated on August 12, 2020

Comments

  • FFish
    FFish over 3 years

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

    <?php           
        $string = "très agréable";  
        // to the database 
        $j_encoded = json_encode(utf8_encode($string)); 
        // get from Database 
        $j_decoded = json_decode($j_encoded); 
    ?>    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
        <?= $j_decoded ?>
    </html> 
    
  • Pekka
    Pekka over 13 years
    Why would one need utf8_decode() (which converts to ISO-8859-1) in a UTF-8 environment?
  • FFish
    FFish over 13 years
    ok, I see. I have to utf8_decode() as well.. Is there a difference doing utf8_decode(json_decode($j_encoded)) vs json_decode(utf8_decode($j_encoded))?
  • teemitzitrone
    teemitzitrone over 13 years
    yes it is, and to be correct you shouldn't use utf8_encode anyway. but the way you used it is the poit. 1 you encode utf8 then json so to get your input you have to decode json and then utf8
  • teemitzitrone
    teemitzitrone over 13 years
    @Pekka if you mess up encoding anyway (see the utf8_encode) you have to correct it. ok, don't mess up the encoding is also a solution and i've edited my answer to reflect that
  • FFish
    FFish over 13 years
    I think this is the best answer, I need to check my DB encoding!
  • Pedro Moreira
    Pedro Moreira almost 10 years
    You could add the code needed to accomplish all of that.
  • Lukas Liesis
    Lukas Liesis almost 9 years
    why giving minus? I had enough situations where this is THE only way that worked. Don't tell me all the stuff about encoding of file, database etc. There are situations you don't know your resource's encoding and it comes in random. Some utf8 some any other you can imagine.
  • Ariel
    Ariel almost 8 years
    Only thing that worked for me, i'm building an API and i need to purely print the response as json with encoded chars
  • CPHPython
    CPHPython over 7 years
    Thanks Lukas, this was exactly what I was looking for. It converts encoding such as \u00e9 into é. I just confirmed its usage in the PHP docs (example 2). I am still curious though, is the depth parameter really useful? If the recursion is stopped at some depth, does it mean the json will not be fully en/decoded according to the bitmask?
  • randomsock
    randomsock about 6 years
    Thanks, this pointed the way for me, except it made things worse at first! :) then I realized that was because something had obviously been "over encoded" somewhere deeper in the stack, so - perhaps strangely - changing the utf8_encode to utf8_decode solved it.
  • Peter Krauss
    Peter Krauss over 5 years
    See also this answer, use JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES
  • WiiLF
    WiiLF about 2 years
    utf8_decode() is always the go to. Beyond that, your looking at mb_convert_encoding() or my preferred - iconv extension.