Thanh Trung
Thanh Trung

Reputation: 3804

PHP encoding in utf-8 and json_encode

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

Upvotes: 0

Views: 467

Answers (1)

Oste Hovel
Oste Hovel

Reputation: 36

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

Upvotes: 2

Related Questions