Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does json_encode of a whitespace string adding it in an array?

I've stubmled across this behavior on PHP 5.6 (also identical in PHP 5.4 up to 7.0).

$note = new SimpleXMLElement('<Note></Note>');
$note->addChild("string0", 'just a string');
$note->addChild("string1", "abc\n\n\n");
$note->addChild("string2", "\tdef");
$note->addChild("string3", "\n\n\n");
$note->addChild("string4", "\t\n");

$json = json_encode($note, JSON_PRETTY_PRINT);

print($json);

Outputs:

{
    "string0": "just a string",
    "string1": "abc\n\n\n",
    "string2": "\tdef",
    "string3": {
        "0": "\n\n\n"
    },
    "string4": {
        "0": "\t\n"
    }
}

There must be a reason behind this behavior, I would like to understand. And also, if you know of a way to force it to behave the same way for strings of texts and whitespace I would appreciate you sharing your ideas!

Edit. Here's a snippet you can run: http://sandbox.onlinephpfunctions.com/code/d797623553c11b7a7648340880a92e98b19d1925

like image 838
Vallieres Avatar asked Nov 09 '22 12:11

Vallieres


1 Answers

This comes from RFC 4627 (emphasis mine)

All Unicode characters may be placed within the quotation marks except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).

Newline(\n) is U+000A in UTF-8 so PHP dutifully converts it back to its respective JS equivalent

PHP uses this RFC for json_encode

PHP implements a superset of JSON as specified in the original » RFC 4627 - it will also encode and decode scalar types and NULL.

As I noted in the comments, all versions of PHP, going back to 5.2, do it this way(Demo)

like image 159
Machavity Avatar answered Nov 14 '22 22:11

Machavity