Json_encode will not encode French characters

I am trying to work with French characters. For some reason, PHP will not output them correctly unless I force the ISO-8859-1 character set (which I really don't want to do); he will not let me use UTF-8.

Thus, when using print_r() my array while forcing ISO-8859-1 you will get 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="#"> 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 follows:

 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="#"> 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 cases, executing json_encode() gives 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 installed in UTF-8, but for some strange reason, every time I insert something with French characters, it goes back to some Western European encodings.

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

Any help would be greatly appreciated.

+2
source share
5 answers

I managed to figure it out. This is not the solution I wanted, but it works. I had to tweak my query to look like this:

 CONVERT(CAST(langselect as BINARY) USING latin1) as langselect 
+2
source

Please, in addition to database encoding, make sure that you check the following:

  • utf8 encoding 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, the forced use of utf8 in a file is necessary when the required encoded values ​​require encoding. You will need to add a comment on top of your php / js / xml file, for example using charset=utf-8 , so ultra-editing or your favorite editor can detect it.

Rgds.

+4
source

base; Does your .php file have a line at the top?

header ("Content-Type: Text / HTML, UTF-8 encoding = \ n");

+2
source

Try working with sources directly in utf8.

I ran into the same problem and used this simple solution: when you create a new PHP or Javascript source file, make sure it is encoded in utf8. I use Ultra Edit and choose the option "UTF8 no bom". This simple step resolved all my problems with encoding characters.

All modern browsers support utf8, so encode your web pages with this. For compatibility, it's easier if all of your source files are also in utf8.

It complies with web standards.

+1
source

I ran into the same problem, but I would suggest:

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

All Articles