2

I know that json_encode requires UTF-8 encoding , and i'm already using that , but at my quest of passing php array to javascript, i can't transfer data as it is.

I put these for test in a null template html page , between JS tags.

$arrs=array("kağıtşim","üğçöşiö");
echo 'data_labels=json_encode($arrs);';

//The result is  ; 
data_labels=["ka\u00c4\u009f\u00c4\u00b1t\u00c5\u009fim","\u00c3\u00bc\u00c4\u009f\u00c3\u00a7\u00c3\u00b6\u00c5\u009fi\u00c3\u00b6"];

There is nothing wrong as i know ; File is UTF-8 Without BOM charset is UTF-8 Headers set to UTF-8 Nothing to do with DB

How can i revert this json_encode conversion

Erdinç Çorbacı
  • 1,187
  • 14
  • 17
  • 2
    The browser will do it for you when it parses the JavaScript. – Quentin Nov 15 '12 at 15:06
  • With `JSON_UNESCAPED_UNICODE` for example. You need to explain why you don't want to be standards-compliant though. – mario Nov 15 '12 at 15:06
  • @mario — What standard would using `JSON_UNESCAPED_UNICODE` violate? – Quentin Nov 15 '12 at 15:06
  • possible duplicate of [Cyrillic characters in PHP's json\_encode](http://stackoverflow.com/questions/410704/cyrillic-characters-in-phps-json-encode) – mario Nov 15 '12 at 15:10
  • @Quentin Thank you for reply... sorry but browser can not do anything because code is dumped in JS block by PHP at server (just one request for page - no ajax etc.) – Erdinç Çorbacı Nov 15 '12 at 15:10
  • @ErdinçÇorbacı — You're generating a JS block … and not giving it to a browser? So you're giving it to node.js or some other engine? Whatever JS engine you use, it will parse the escape sequences. That's a large part of the reason JSON was designed as a subset of JS. – Quentin Nov 15 '12 at 15:12
  • @mario yes similar problem , but i cannot express my full utf8 that might make a difference ... also that is not solved and not asked as clear as this. – Erdinç Çorbacı Nov 15 '12 at 15:14
  • @Quentin sorry my friend i got you wrong i think. Surely server sends html file with these code in it. And Javascript interpreter runs that code. But that file has encoded string in it. So JS returns error – Erdinç Çorbacı Nov 15 '12 at 15:17
  • @Quentin I got that now .... after debug i saw what you mean i wish to +1 you my friend ... indedd i did in my heart :) – Erdinç Çorbacı Nov 15 '12 at 15:22
  • @Quentin: No, the browser can't handle that; it expects each Unicode-escape (`\uXXXX`) to be a single code-point (or, more properly: a single UTF-16 code-unit), but the OP is getting each UTF-8 code-unit (i.e., each byte of the UTF-8-encoded string) as a separate Unicode-escape. – ruakh Nov 15 '12 at 15:35
  • @ruakh Sorry but Quertin was right, with result code in an html file , after that line javascript debug shows the decoded values. As accepted answer shows ... – Erdinç Çorbacı Nov 15 '12 at 15:45

1 Answers1

3

You do not need to revert anything. When the JavaScript parser parses those strings it automatically converts those escape sequences to proper characters.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636