json_encode won't encode French characters

I’m trying to work with French characters. For whatever reason, PHP won’t output them correctly unless I force the ISO-8859-1 character set (which I really don’t want to do); it won’t let me use UTF-8.

So doing a print_r() of my array when I force ISO-8859-1 yields the following:

Array
(
[0] => stdClass Object
    (
        [language] => fr
        [langselect] => î
        [s1next] => Prochain
        [hascodespan] => Avez-vous déjà un code d'enregistrement?
        [s2prev] => Précédent
        [s2next] => Oui, j'ai déjà un code.
        [ecodedescription] => Un E-Code vous donne un accès exclusif à des événements vraiment cool. Si vous êtes dans le besoin d'un code, vous pouvez en acheter un dans notre boutique en ligne en visitant ce lien <a href="#" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"> boutique en ligne</ a>.
        [purchase] => Cliquez ici pour acheter en ligne billets
        [ecodespan] => S'il vous plaît entrer votre e-code
        [ecodelocdescription] => Votre code peut être trouvé ci-dessous le code à barres sur votre billet
        [s3prev] => Précédent
        [s3next] => Prochain
        [validationtext] => Validation E-Code ... Un instant.
    )

)

When using UTF-8, the output is as following:

Array
(
[0] => stdClass Object
    (
        [language] => fr
        [langselect] => �
        [s1next] => Prochain
        [hascodespan] => Avez-vous d�j� un code d'enregistrement?
        [s2prev] => Pr�c�dent
        [s2next] => Oui, j'ai d�j� un code.
        [ecodedescription] => Un E-Code vous donne un acc�s exclusif � des �v�nements vraiment cool. Si vous �tes dans le besoin d'un code, vous pouvez en acheter un dans notre boutique en ligne en visitant ce lien <a href="#" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"> boutique en ligne</ a>.
        [purchase] => Cliquez ici pour acheter en ligne billets
        [ecodespan] => S'il vous pla�t entrer votre e-code
        [ecodelocdescription] => Votre code peut �tre trouv� ci-dessous le code � barres sur votre billet
        [s3prev] => Pr�c�dent
        [s3next] => Prochain
        [validationtext] => Validation E-Code ... Un instant.
    )

)

In both instances, executing a json_encode() yields the following result:

[
    {
        language: "fr",
        langselect: null,
        s1next: "Prochain",
        hascodespan: null,
        s2prev: null,
        s2next: null,
        ecodedescription: null,
        purchase: "Cliquez ici pour acheter en ligne billets",
        ecodespan: null,
        ecodelocdescription: null,
        s3prev: null,
        s3next: "Prochain",
        validationtext: "Validation E-Code ... Un instant."
    }
]

I have my database set to UTF-8, yet for some odd reason every time I insert anything with French characters it reverts itself to some Western European encoding.

Mainly, I really need json_encode() to return valid results so I can use it in my translations. I have tried iconv() and utf8_encode(), but to no avail.

Any help would be greatly appreciated.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Please, besides the database encoding, be sure you check the following:

  • utf8 encoding of the FILES (js/php)
  • utf8 html content: <meta HTTP-EQUIV="content-type" CONTENT="text/html; charset=utf-8">
  • utf8 of your db connection: SET character_set_connection = 'utf8'
  • utf8 of your mysql tables: ALTER TABLE table CONVERT TO CHARACTER SET utf8;
  • utf8 of your query results: SET character_set_results = 'utf8'
  • utf8 of your db client: SET character_set_client = 'utf8'
  • utf8 of your db server: SET character_set_database = 'utf8' and SET character_set_server = 'utf8'
  • in some cases, forcing utf8 in file is necessary when hardcoded values need encoding. You would need to add a comment on top of your php/js/xml file for instance, with charset=utf-8, so ultra edit or your favorite editor can detect it.

rgds.

Solution 2

basic; has your .php file the line on top?

header(“content-type:text/html;charset=utf8\n”);

Solution 3

I managed to figure it out. It’s not really the solution I wanted but it works. I had to adjust my query to look like:

CONVERT(CAST(langselect as BINARY) USING latin1) as langselect

Solution 4

Try to work on sources directly in utf8.

I was faced with the same problem, and used this simple solution: When you create a new PHP or Javascript source file, ensure it is encoded in utf8.
I use Ultra Edit, and chose the option “UTF8 no bom”. This simple step resolved all my problems with encoding characters.

All modern browsers support utf8, so encode your webpages with this. For compatibility, it is simpler if all your source files are also in utf8.

This follows web standards.

Solution 5

I have run into the same issue, but I would suggest:

$array = htmlentities($array);
$json = json_encode($array);

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply