Json utf8 php

How can I save a json-encoded string with international characters to the databse and then parse the decoded string in the browser?


Pretty much the way you show. What doesn't work? – Pekka Nov 2 "10 at 11:01
it gives me: très agréable – FFish Nov 2 "10 at 11:05

Try setting the charset for your page to UTF-8... in the bloông chồng – Mark Baker Nov 2 "10 at 11:08

Mark, that worked, but only when using $j_encoded = json_encode($string); so without the utf8_encode() – FFish Nov 2 "10 at 11:17

FFish get rid of the utf8_encode() in your code, I just saw that (Edit: ah, you were quicker) – Pekka Nov 2 "10 at 11:17

This is an encoding issue. It looks lượt thích at some point, the data gets represented as ISO-8859-1.

Bạn đang xem: Json utf8 php

Every part of your process needs lớn be UTF-8 encoded.

The database connection

The database tables

Your PHP tệp tin (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 check my DB encoding! – FFish Nov 2 "10 at 12:18

You could add the code needed to lớn accomplish all of that. – Pedro Moreira Jun 29 "14 at 14:54


json 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 tệp tin, 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 to 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? – CPHPynhỏ nhắn Aug 29 "16 at 18:11
See also this answer, use JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES – Peter Krauss Oct 8 "18 at 8:00

If your source-tệp tin 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 AND you should add an http header which sets the transferencoding to lớn utf-8.

and in php

Try sending the UTF-8 Charphối 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 lớn 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à khổng lồ 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 to get your input đầu vào you have lớn decode json and then utf8 – teemitzitrone Nov 2 "10 at 11:25
Pekka if you mess up encoding anyway (see the utf8_encode) you have 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:30


For me both methods

if you get "unexpected Character" error you should kiểm tra if there is a BOM (Byte Order Marker saved inkhổng lồ your utf-8 json. You can either remove the first character or save if without BOM.

Xem thêm: Bệnh Thèm Đàn Ông Phải Là Bệnh 'Thiếu Hơi Trai', Hysteria Không Phải Là Bệnh “Thiếu Hơi Trai”


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 staông xã, so - perhaps strangely - changing the utf8_encode to utf8_decode solved it. – randomsoông xã Feb 16 "18 at 20:trăng tròn


I had the same problem. It might differ depending on how You put the data to lớn the db, but try what worked for me:

$str = json_encode($data); $str = addslashes($str);

Do this before saving data khổng lồ db.


Thanks for contributing an answer lớn Stachồng Overflow!

Please be sure lớn answer the question. Provide details and nội dung your research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based on opinion; baông chồng them up with references or personal experience.

To learn more, see our tips on writing great answers.


Post Your Answer Discard

By clicking “Post Your Answer”, you agree lớn our terms of service, privacy policy and cookie policy


Not the answer you're looking for? Browse other questions tagged php utf-8 json or ask your own question.


How lớn demo variable values which differ due to floating point inaccuracy caused by different DBMS types?
Do the observations of constitutional courts, which are not necessary for deciding the case, size binding precedents for lower courts?

Creating circular sectors of different radius at different angles more hot questions
Staông chồng Overflow Questions Jobs Developer Jobs Directory Salary Calculator Help Mobile
Products Teams Talent Advertising Enterprise
Company About Press Work Here Legal Privacy Policy Terms of Service Liên hệ Us Cookie Settings Cookie Policy
Staông xã Exchange Network Technology Life / Arts Culture / Recreation Science Other
Stack Overflow Server Fault Super User Web Applications Ask Ubuntu Webmasters Game Development
TeX - LaTeX Software Engineering Unix & Linux Ask Different (Apple) WordPress Development Geographic Information Systems Electrical Engineering
Android Enthusiasts Information Security Database Administrators Drupal Answers SharePoint User Experience Mathematica
Salesforce ExpressionEngine® Answers Staông chồng Overflow em Português Blender Network Engineering Cryptography Code Reviews
Magenlớn Software Recommendations Signal Processing Emacs Raspberry Pi Stachồng Overflow на русском Code Golf
Staông xã Overflow en español Ethereum Data Science Arduino Bitcoin Software Quality Assurance & Testing Sound Design
Windows Phone more (28)
Photography Science Fiction & Fantasy Graphic Design Movies & TV Music: Practice & Theory Worldbuilding Video Production
Seasoned Advice (cooking) trang chủ Improvement Personal Finance & Money Academia Law Physical Fitness Gardening & Landscaping
Parenting more (10)
English Language & Usage Skeptics Mi Yodeya (Judaism) Travel Christianity English Language Learners Japanese Language
Chinese Language French Language German Language Biblical Hermeneutics History Spanish Language Islam
Русский язык Russian Language Arqade (gaming) Bicycles Role-playing Games Anime & Manga Puzzling
Motor Vehicle Maintenance & Repair Board & Card Games Bricks Homebrewing Martial Arts The Great Outdoors Poker
Chess Sports more (16)
MathOverflow Mathematics Cross Validated (stats) Theoretical Computer Science Physics Chemistry Biology
Computer Science Philosophy Linguistics Psychology & Neuroscience Computational Science more (10)

Blog Facebook Twitter LinkedIn Instagram

site thiết kế / logo © 2021 Stachồng Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.6.38987



Your privacy

By clicking “Accept all cookies”, you agree Staông chồng Exchange can store cookies on your device & discthảm bại information in accordance with our Cookie Policy.


Accept all cookies Customize settings