-1

I get two different results for

json_encode([ 'name' => 'xxx❤xxx' ]);

--> {"name":"xxx\u2764xxx"}

JSON.stringify({ name: 'xxx❤xxx');

--> {"name":"xxx❤xxx"}

Why is that and how can I make sure that the js version produces the same result as the php version?

Chris
  • 13,100
  • 23
  • 79
  • 162
  • 1
    They're the same: https://codepoints.net/U+2764?lang=en It's just that for some reason one is showing the rendered emoji and one just showing the codepoint. Depends on what environment you're viewing the results in. – Robin Zigmond Feb 25 '19 at 12:49
  • 2
    Sorry if you're already aware of it but it's worth nothing that both output blocks contain the exactly same information, it's merely that the PHP code has been configured to produce plain US-ASCII output rather than UTF-8. – Álvaro González Feb 25 '19 at 12:50

1 Answers1

0

The escaping in PHP is optional but not technically required for valid JSON (which can contain arbitrary Unicode aside from a few reserved whitespace characters). The feature can be turned off with json_encode($data, JSON_UNESCAPED_UNICODE).

Unfortunately, the JS version doesn't have the feature at all. If you want to escape multibyte characters to \u...., you should do it explicitly; see JSON.stringify and unicode characters.

Christoph Burschka
  • 4,467
  • 3
  • 16
  • 31