Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a JSON into a UTF-8 string

Tags:

json

string

php

I want to convert a JSON object into a string. when I am using json_encode I get a string but all with hex letters. I want to convert it to a UTF-8. In other words I want to see the characters. How do I do it?

like image 200
nisnis Avatar asked Dec 12 '22 17:12

nisnis


1 Answers

I was using json_encode to store data such as Arabic Characters in MySQL fields.

It would store the Arabic characters as HEX within the Database like this:

u0644 u063a...

Which is incorrect. You must ensure that you wrap your json_encode with mysql_escape_string().

This will make sure that the data is put in MySQL as:

\u0644\u063a...

Then, when you use json_decode, it converts the HEX strings into UTF-8 and is output correctly.

like image 167
Moe Avatar answered Dec 27 '22 08:12

Moe