json_encode and persian words?

梦想与她 提交于 2020-01-15 02:58:28

问题


I am using json_encode for inserting a value array (like: <input name="ok[]">... in the database, don't know why it inserted Persian words as ["\u0633\u06cc\u062f \u0633\u0639\u06cc\u062f \u062f\u0627\u062f\u0627\u0634\u0632\u0627\u062f\u0647"] it was earlier inserted as: سید سعید داداشزاده.

Output of database (select * from tabla ...) by json_encode is as:[\"\\u0633\\u06cc\\u062f \\u0633\\u0639\\u06cc\\u062f \\u062f\\u0627\\u062f\\u0627\\u0634\\u0632\\u0627\\u062f\\u0647\"]"

In the my table (of database), Collation of this row is utf8_general_ci?

What do I do for print("output of database") Persian words as سید سعید داداشزاده ?


回答1:


json_encode encodes all non-ascii characters with the \uXXXX notation. This is not a problem, because any json decoder, and javascript, recognize this notation:

json_decode('["\u0633\u06cc\u062f \u0633\u0639\u06cc\u062f \u062f\u0627\u062f\u0627\u0634\u0632\u0627\u062f\u0647"]');
// array('سید سعید داداشزاده')

However, it seems that the string that you get from the database is escaped. Either it has been double-escaped before inserting in the database, or you have magic_quotes_runtime enabled. Use stripslashes on the json string, before using json_decode, to un-escape it:

json_decode(stripslashes('[\"\\u0633\\u06cc\\u062f \\u0633\\u0639\\u06cc\\u062f \\u062f\\u0627\\u062f\\u0627\\u0634\\u0632\\u0627\\u062f\\u0647\"]'));



回答2:


just use this

json_encode($array,JSON_UNESCAPED_UNICODE)

work fine !!




回答3:


json_encode is escaping each character. Use stripslashes() to the string to remove the extra slash for each character.




回答4:


you can use this gist:

https://gist.github.com/MahdiMajidzadeh/88407f4c33a294cae29ed1493332d7c0

:)))))



来源:https://stackoverflow.com/questions/7436427/json-encode-and-persian-words

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!