How to send string encoded in utf8mb4_unicode_ci in JSON from PHP to Node.js ?
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:
éèàö 😀 😎
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:
éèàö 😀 😎
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:
éèàö 😀 😎
# | ID | Query | URL | Count |
---|