2

I have an array, with a lot of characters from languages like Russian. When I try to use json_encode to that array, it returns me null.

However, I have a workaround for that, it is to use:

array_map("utf8_encode", $arr );

On the array and then json_encode for the new array. It works, but it gives me some crazy characters like this:

поÑÑеа ÑигниÑеÑÑмÑÑе но веÐ

Instead of russian letters. I have no idea how to fix it and make it work and return real Russian letters as the output. I have tried using json_encode with JSON_UNESCAPED_UNICODE as second parameter, on main array, but it does not work. I have even set the encoding of the database using:

mysqli_set_charset($db,"utf8");

But even that does not fix it. I am totally out of ideas and I don't know what to do.

Background story is that I input the data of array into html (php to json and than javascript to html) but if I don't use array_map, it returns null. If I use it, I have crazy characters all over my html.

Please if anyone has any ideas, share with me, i have no idea what to do.

EDIT: I have solved my problem. However, I did not use JSON at all. I just packed the data into the string with special spliting code. Than, on the client side, I have used string.split("spliting code") in order to get the parts of the string. It works with any encoding.

Nikola Pejic
  • 145
  • 1
  • 12
  • 1
    Does [`json_last_error`](http://php.net/manual/en/function.json-last-error.php) show anything? – Script47 Sep 13 '17 at 13:10
  • 3
    Possible duplicate of ['Malformed UTF-8 characters, possibly incorrectly encoded' in Laravel](https://stackoverflow.com/questions/31115982/malformed-utf-8-characters-possibly-incorrectly-encoded-in-laravel) – chad Sep 13 '17 at 13:12
  • It gives me Malformed UTF-8 error, key 5 – Nikola Pejic Sep 13 '17 at 13:17
  • `utf8_encode()` is totally pointless when dealing with Russian. Please check the manual to find out what it really does, don't just trust its name because it's a poorly named function. – Álvaro González Sep 20 '17 at 12:18

0 Answers0