简体   繁体   中英

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 to display it correctly in my page but I don't know why it is converted into instead

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM