Converting Newlines in JSON array to line breaks: JSON_decode and nl2br

dabra904 picture dabra904 · Jan 31, 2014 · Viewed 7k times · Source

I cannot get the below array to replace the carriage return and newline with an HTML break <br />

JSON Array:

{"Fruit":"Apple","Veggies":"Carrot\r\nCelery\r\n\r\nCucumbers"}

I'm trying this and having no luck:

$html = json_decode(nl2br($json),true);

The line breaks are visible in the source code of the HTML itself however I cannot get the <br /> code to replace the 'newlines'.

I've tried this suggestion as well and it did not work either: Replacing \r\n (newline characters) after running json_encode

Answer

dabra904 picture dabra904 · Jan 31, 2014

Run nl2br() after JSON is decoded, not on the JSON array source.