How can I save a json-encoded string with international characters lớn the databse & then parse the decoded string in the browser?
This is an encoding issue. It looks like at some point, the data gets represented as ISO-8859-1.Bạn đã xem: Json utf8 php
Every part of your process needs khổng lồ be UTF-8 encoded.
Bạn đang xem: Php json_encode json_decode utf
The database connection
The database tables
Your PHP file (if you are using special characters inside that tệp tin as shown in your example above)
The content-type headers that you output
I think this is the best answer, I need to kiểm tra my DB encoding! – FFish Nov 2 "10 at 12:18 You could địa chỉ cửa hàng the code needed to lớn accomplish all of that. – Pedro Moreira Jun 29 "14 at 14:54json utf8 encode and decode:
json_encode($data, JSON_UNESCAPED_UNICODE) json_decode($json, false, 512, JSON_UNESCAPED_UNICODE)force utf8 might be helpfull too: http://pastebin.com/2XKqYU49
why giving minus? I had enough situations where this is THE only way that worked. Don't tell me all the stuff about encoding of file, database etc. There are situations you don't know your resource's encoding & it comes in random. Some utf8 some any other you can imagine. – Lukas Liesis Jul 28 "15 at 10:23 Only thing that worked for me, i'm building an API & i need lớn purely print the response as json with encoded chars – Ariel Jun 17 "16 at 20:02 Thanks Lukas, this was exactly what I was looking for. It converts encoding such as u00e9 into é. I just confirmed its usage in the PHP docs (example 2). I am still curious though, is the depth parameter really useful? If the recursion is stopped at some depth, does it mean the json will not be fully en/decoded according lớn the bitmask? – CPHPython Aug 29 "16 at 18:11 See also this answer, use JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES – Peter Krauss Oct 8 "18 at 8:00If your source-file is already utf8 then drop the utf8_* functions. Php5 is storing strings as array of byte.
you should add a meta tag for encoding within the html & you should địa chỉ an http header which sets the transferencoding khổng lồ utf-8.
and in php
Try sending the UTF-8 Charset header:
And the HTML meta:
utf8_decode $j_decoded = utf8_decode(json_decode($j_encoded)); EDIT or khổng lồ be more correct $j_encoded = json_encode($j_encoded); $j_decoded = json_decode($j_encoded); no need for en/decoding utf8 Why would one need utf8_decode() (which converts to ISO-8859-1) in a UTF-8 environment? – Pekka Nov 2 "10 at 11:15 ok, I see. I have lớn utf8_decode() as well.. Is there a difference doing utf8_decode(json_decode($j_encoded)) vs json_decode(utf8_decode($j_encoded))? – FFish Nov 2 "10 at 11:19 yes it is, và to be correct you shouldn't use utf8_encode anyway. But the way you used it is the poit. 1 you encode utf8 then json so khổng lồ get your input đầu vào you have lớn decode json & then utf8 – teemitzitrone Nov 2 "10 at 11:25 Pekka if you mess up encoding anyway (see the utf8_encode) you have to lớn correct it. Ok, don't mess up the encoding is also a solution and i've edited my answer to lớn reflect that – teemitzitrone Nov 2 "10 at 11:30For me both methods
Thanks, this pointed the way for me, except it made things worse at first! :) then I realized that was because something had obviously been "over encoded" somewhere deeper in the stack, so - perhaps strangely - changing the utf8_encode lớn utf8_decode solved it. – randomsock Feb 16 "18 at 20:20I had the same problem. It might differ depending on how You put the data lớn the db, but try what worked for me:
$str = json_encode($data); $str = addslashes($str);Do this before saving data to db.
Xem thêm: Cô Gái Thử Dùng 3 Cây Lăn Mặt Vline Đẹp, Chính Hãng Chất Lượng, Giá Rẻ Hấp Dẫn
Thanks for contributing an answer lớn Stack Overflow!
Please be sure lớn answer the question. Provide details & share your research!But avoid …
Asking for help, clarification, or responding khổng lồ other answers.Making statements based on opinion; back them up with references or personal experience.To learn more, see our tips on writing great answers.
Post Your Answer DiscardBy clicking “Post Your Answer”, you agree lớn our terms of service, privacy policy & cookie policy
Not the answer you're looking for? Browse other questions tagged php utf-8 json or ask your own question.
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device và disclose information in accordance with our Cookie Policy.