I skipped this behavior in PHP 5.6 (also identical in PHP 5.4 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 for this behavior, I would like to understand. And also, if you know a way to make it behave the same for lines of texts and spaces, I would appreciate if you share your ideas!
Change Here is a snippet you can run: http://sandbox.onlinephpfunctions.com/code/d797623553c11b7a7648340880a92e98b19d1925
source share