Question #7120   Submitted by Answiki on 01/06/2023 at 01:29:42 PM UTC

How to send string encoded in utf8mb4_unicode_ci in JSON from PHP to Node.js ?

Answer   Submitted by Answiki on 01/06/2023 at 01:37:14 PM UTC

PHP side


The PHP function json_encode() only accept utf8 strings. The string must be escaped before converted to json:

$str = "éèàö 😀 😎";
myJson = json_encode(utf8_encode($str));

The above code produces the following output:

"\u00c3\u00a9\u00c3\u00a8\u00c3\u00a0\u00c3\u00b6 \u00f0\u009f\u0098\u0080 \u00f0\u009f\u0098\u008e"


Node.js side


On Node.js side, install the utf8 package:

npm install utf8

Decode the string with utf8.decode(str):

const utf8 = require('utf8');

str = "\u00c3\u00a9\u00c3\u00a8\u00c3\u00a0\u00c3\u00b6 \u00f0\u009f\u0098\u0080 \u00f0\u009f\u0098\u008e";
console.log(utf8.decode(str));

The above code displays the original string:

éèàö 😀 😎

3 events in history
Answer by Answiki on 01/06/2023 at 01:37:14 PM

PHP side


The PHP function json_encode() only accept utf8 strings. The string must be escaped before converted to json:

$str = "éèàö 😀 😎";
myJson = json_encode(utf8_encode($str));

The above code produces the following output:

"\u00c3\u00a9\u00c3\u00a8\u00c3\u00a0\u00c3\u00b6 \u00f0\u009f\u0098\u0080 \u00f0\u009f\u0098\u008e"


Node.js side


On Node.js side, install the utf8 package:

npm install utf8

Decode the string with utf8.decode(str):

const utf8 = require('utf8');

str = "\u00c3\u00a9\u00c3\u00a8\u00c3\u00a0\u00c3\u00b6 \u00f0\u009f\u0098\u0080 \u00f0\u009f\u0098\u008e";
console.log(utf8.decode(str));

The above code displays the original string:

éèàö 😀 😎

Answer by Answiki on 01/06/2023 at 01:36:48 PM

PHP side


The PHP function json_encode() only accept utf8 strings. The string must be escaped before converted to json:

$str = "éèàö 😀 😎";
myJson = json_encode(utf8_encode($str));

The above code produces the following output:

"\u00c3\u00a9\u00c3\u00a8\u00c3\u00a0\u00c3\u00b6 \u00f0\u009f\u0098\u0080 \u00f0\u009f\u0098\u008e"


Node.js side


On Node.js side, install the utf8 package:

npm install utf8

Decode the string with utf8.decode(str):

const utf8 = require('utf8');

str = "\u00c3\u00a9\u00c3\u00a8\u00c3\u00a0\u00c3\u00b6 \u00f0\u009f\u0098\u0080 \u00f0\u009f\u0098\u008e";
console.log(utf8.decode(str));

The above code displays the original string:

éèàö 😀 😎

Question by Answiki 01/06/2023 at 01:29:42 PM
How to send string encoded in utf8mb4_unicode_ci in JSON from PHP to Node.js ?
# ID Query URL Count

Icons proudly provided by Friconix.