开发者

json_encode and persian words?

开发者 https://www.devze.com 2023-04-05 17:44 出处:网络
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\\u

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 \\u开发者_Python百科0633\\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 سید سعید داداشزاده ?


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\"]'));


just use this

json_encode($array,JSON_UNESCAPED_UNICODE)

work fine !!


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


you can use this gist:

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

:)))))

0

精彩评论

暂无评论...
验证码 换一张
取 消