20

I'm developing a dependent select script using jQuery, PHP and JSON as the response.

Everything goes well except for using special characters like French ones (é , è , à...)

if I pre-encode them like (é , è , à) (Here I'm using spaces between the ampersand and the rest of the word to prevent auto encoding in my question) it works but when rendered with jquery the characters are not converted to what they should look like (é...), instead they are shown as is (é)

If I write them like (é) and don't pre-encode them the full value in this array entry is not shown.

What should I do here?

Thanks.

Matt
  • 74,352
  • 26
  • 153
  • 180
medk
  • 9,233
  • 18
  • 57
  • 79
  • 1
    There's usually no need to entitize stuff since jQuery does it for you. – NullUserException Sep 02 '11 at 14:04
  • 1
    @NullUserException: It's *not* jQuery that "entitizes your stuff". It is not even entitized at all. – Tomalak Sep 02 '11 at 14:10
  • @Tomalak Hmmm... I am referring to `.text()`, which does convert certain characters to their HTML entity equivalents (via `createTextNode()`) – NullUserException Sep 02 '11 at 14:18
  • @NullUserException: No, it does not. `text()` sets the `textContent` property of a DOM node. HTML character entities only exist in serialized HTML (i.e. when HTML is represented as text), they do not even exist in the DOM. – Tomalak Sep 02 '11 at 15:06

3 Answers3

10

If I write them like (é) and don't pre-encode them the full value in this array entry is not shown.

What should I do here?

In JSON you do not HTML-encode values. You send them literally (é) and set set Content-Type correctly:

header('Content-Type: application/json; Charset=UTF-8');

Declare the encoding your data is in, of course.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • ok I just added this header before the json_encode() in the file that will send the json response and tried both ways (é and &eacute) but still the same problem – medk Sep 02 '11 at 14:09
  • @medk: First off: Stop trying with `&eacute`. You are not dealing with HTML here so this has nothing to do with your problem *at all*. Have you just tried that header unmodified or did you use the actual encoding of your data? – Tomalak Sep 02 '11 at 14:12
  • yes I explicitly set the encoding to utf-8 in firefox for the page that shows the results and tried a response that contains the two types (normal and encoded), the normal never shows and the pre-encoded shows as is, so always the same problem – medk Sep 02 '11 at 14:17
  • @medk: What you set the encoding to in Firefox does not matter. You must make sure that the server *sends* the characters in the advertised encoding: When the header says UTF-8, the data must be UTF-8. If your data *is not* UTF-8 (and I suspect it isn't), then you must change the header accordingly. – Tomalak Sep 02 '11 at 16:03
7

This worked for me, hopefully it will work for anyone else experiencing similar issues.

$title = 'é';
$title = mb_convert_encoding($title, "UTF-8", "HTML-ENTITIES");

header('Content-Type: application/json; Charset="UTF-8"');
echo json_encode(array('title' => $title));

The mb_convert_encoding function takes a value and converts it from (in this case) HTML-ENTITIES to UTF-8.

See here for me details on the function http://php.net/manual/en/function.mb-convert-encoding.php

rharvey
  • 1,987
  • 1
  • 28
  • 23
  • You rock!! Worked for me when converting from MySQL utf.8 to Firebase using a PHP library. – Matt H Oct 22 '17 at 22:30
  • nice man, for my code function this: echo json_encode(mb_convert_encoding($this->jsonRet, "HTML-ENTITIES", "UTF-8")); – Rinos Aug 22 '18 at 13:28
6

Just like the first anwser

Do you use a database? If Yes, make sure the database table is declared UFT8 How is declared the HTML page? UTF-8 IS the string in the PHP script file? If yes, make sure the file has a UTF-8 file format

You could also use utf8_encode (to send to HTML) and utf8_decode (to receive) but not the right way

Rahul
  • 12,886
  • 13
  • 57
  • 62
YvonBlais
  • 125
  • 1
  • 6