Home > Software design >  PHP equivalent of javascript JSON.stringify()
PHP equivalent of javascript JSON.stringify()

Time:02-14

As I notice PHP's json_encode($array) mess things up on diacritics. If I update my database column type text with javascript-created JSON passed over HTTP, everything looks good. but when I create the JSON into PHP, some characters get encoded weirdly.

I have this array;

$array = ['M-am întîlnit ieri cu','fosta mea profă de matematică'];

$text = implode(",",$array); // this looks good in db
$json = json_encode($array); // this don't and returns error when try to decode later.

CodePudding user response:

Look at the JSON_UNESCAPED_UNICODE flag:

https://www.php.net/function.json-encode

This flag will "encode multibyte Unicode characters literally" according to https://www.php.net/manual/en/json.constants.php.

So you want to do:

json_encode($array, JSON_UNESCAPED_UNICODE);

This will give you the following output:

["M-am întîlnit ieri cu","fosta mea profă de matematică"]

Working example: https://3v4l.org/daETG

  • Related