PDA

View Full Version : Line breaks, them evil line breaks! >.<


Andy Huang
Fri 17th Dec '04, 8:55pm
Hi all,
I am running into a slight bit of a problem here. When an user uses textarea to enter data into MySQL, it stores the line breaks no problem. However, because of the way browsers render HTML, simply having them in there does not display them and I will probably need to convert them into <br /> instead.

Is there any efficient way in doing this? Should I do that for every time something is pulled from the db? or should I do something prior to entering them into db? Any suggestions would be welcomed :)

Cheers,
Alf

daemon
Fri 17th Dec '04, 10:47pm
You can run the text area's contents through nl2br() (http://us2.php.net/nl2br). This will convert all new lines to a HTML break.

As far as when to do it, it depends on a few things. If you want to be able to go back and edit the data without the HTML line breaks showing, I recommend storing the unparsed version in the database and then parsing the line breaks when the page is called. But if you feel that this page will be hit a lot, then you could create another column in MySQL called data_parsed (or something similar) and then store a nl2br()'d version and an unparsed version. If you do not care about the HTML line breaks, you can just store the parsed version.

Andy Huang
Fri 17th Dec '04, 11:18pm
Ah, thanks, I'll go do that right now :)