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

5
  • So what's your question? Commented Jun 24, 2015 at 9:05
  • I cannot convert into \u0152 correctly Commented Jun 24, 2015 at 9: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). Commented Jun 24, 2015 at 9: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 Commented Jun 24, 2015 at 9:15
  • Indeed, convert enconding from MYSQL helps Commented Jun 24, 2015 at 9:26

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.