2

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 \r\n 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":"\r\n Expired\r\n ","expDate":"1999-12-20","dicAct":"\r\n Yes\r\n "}
0

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you--the trim command was the answer. (I'm new the PHP in general--will accept your answer when the timelimit is up)
I'm glad I could help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.