javascriptphpjsonequivalent

PHP equivalent of javascript JSON.stringify()


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.

Solution

  • First off, it's worth pointing out that PHP is not "messing up" anything. It's escaping characters, which may look weird, but it is perfectly valid and when you json_decode it later it will be just the same as it was originally. See here: https://3v4l.org/Smj2F

    If you don't like the escaping though, you can use 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 can do:

    json_encode($array, JSON_UNESCAPED_UNICODE);
    

    And it will give you the following output:

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

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