Jennifer Anthony Jennifer Anthony - 6 months ago 21
PHP Question

json_encode and persian words?

I use of

json_encode
for insert a value array (like:
<input name="ok[]">...
in the database, not know why inserted persian words as
["\u0633\u06cc\u062f \u0633\u0639\u06cc\u062f \u062f\u0627\u062f\u0627\u0634\u0632\u0627\u062f\u0647"]
it is befor insert 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 this row is
utf8_general_ci
?

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

Answer

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