Skip to content
Advertisement

Why is rn appearing when I JSON encode an array

I’m creating some arrays and turning them into JSON strings and I noticed something strange–some of the strings, when I JSON encode them, are getting rn added onto the front and end of the of the strings. The strings I’m encoding are pulled from HTML elements.

JavaScript

Expected output:

JavaScript

Actual output:

JavaScript

Advertisement

Answer

It seems $rows2[13]->nodeValue and $rows2[11]->nodeValue have carry return and line feeds in them.

You can use trim() to get rid of them:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement