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.
$arr = array( 'licStat' => $rows2[13]->nodeValue, 'expDate' => dateReplace($data[5]->nodeValue), 'dicAct' => $rows2[11]->nodeValue ); echo json_encode($arr);
Expected output:
{"licStat":"Expired","expDate":"1999-12-20","dicAct":"Yes"}
Actual output:
{"licStat":"rn Expiredrn ","expDate":"1999-12-20","dicAct":"rn Yesrn "}
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:
$arr = array( 'licStat' => trim($rows2[13]->nodeValue), 'expDate' => dateReplace($data[5]->nodeValue), 'dicAct' => trim($rows2[11]->nodeValue) ); echo json_encode($arr);