0

I have this character Œ which is encoded in iso-8859-1 (latin1_swedish_ci) in database. I want to convert it into utf-8 to use in json_encode

$name = 'CŒUR'; //in iso-8859-1

$data = array('name' => utf8_encode($name));

echo json_encode($data);

Displayed:

{
    "name":"C\u008cUR"
}

Expected:

{
    "name":"C\u0152UR"
}

Then I get the response using AJAX, convert it into json object then display it on the page.

Check Fiddle

The 2nd one is the expected result, you can check in the console.

Test link Here

Question: I want it to convert into \u0152 to display it correctly in my page but I don't know why it is converted into \u008c instead

0yeoj
  • 4,500
  • 3
  • 23
  • 41
Thanh Trung
  • 3,566
  • 3
  • 31
  • 42
  • So what's your question? – Daan Jun 24 '15 at 09:05
  • I cannot convert into \u0152 correctly – Thanh Trung Jun 24 '15 at 09:06
  • 1
    Œ doen't exist in ISO-8859-1. It's a Windows-1252 character. Besides, latin1 for MySQL is not ISO-8859-1 but Windows-1252. Replace `utf8_encode($name)` by `iconv('CP1252', 'UTF-8', $name)` or, better, just ask MySQL to return results set directely encoded in utf8 instead of latin1 (the SET NAMES equivalent - for PDO it's the parameter named *charset* of the DSN since PHP 5.3.6). – julp Jun 24 '15 at 09:11
  • But in my database, in phpmyadmin it shows correctly `Œ` and if I do an `echo 'Œ'` directly, it shows correctly in my web page, in iso8859-1 charset – Thanh Trung Jun 24 '15 at 09:15
  • Indeed, convert enconding from MYSQL helps – Thanh Trung Jun 24 '15 at 09:26

1 Answers1

2

If i understand it correctly it seems like you are recieving data in wrong charset from the database? Set charset to the data recieved from PDO

Community
  • 1
  • 1
Oste Hovel
  • 36
  • 3
  • I used `mysql_set_charset('utf8')` to return UTF-8 data and it solves my problem, thanks. Seems like converting it using PHP encode or decode can't work – Thanh Trung Jun 24 '15 at 09:27