views:

50

answers:

1

Building a web app that includes several contentEditable divs. The users add stuff to the div's, clicks the save button, which saves all the data from each div to a json object. That goes through JSON.stringify to a mysql database as a TEXT (uploaded with php via mysql_real_escape_string() ). When loaded it goes back to JSON.parse and then via javascript and jquery is placed back where it was. The only issue is that newline characters are not replaced.
Is there a quick fix to this?

+3  A: 

New line characters in HTML are ignored. What you need is <br/> tags to replace the newlines, and PHP has the perfect function for you. nl2br() will replace the newlines with these tags and should return the output you're expecting.

$string = "some text\nwith\nnewlines";
echo nl2br($string);
// should return "some text<br/>with<br/>newlines"
thetaiko