PHPScriptor wrote:

How do you guys handle this "problem".

Just a form with a textarea. When I use enters in the textarea it's saved to
the db like this:

"first line
second line"

when I edit the value in the form:
"first line
second line"

when I output the value to html:
"first linesecond line" (unless I use nl2br())

Is there a way that I could save it to the db that will work for db, output
and edit without using any other function like nl2br?

I run all text area through a filter that removes the carriage return [ preg_replace('/\r/','',$input) ] and save it in the database with any newline characters.

When calling data from the database, I can then use newline as a delimiter for explode generate an array where each element of the array is a distinct line of text - and then do whatever I want with the lines.

Effectively what I usually do is the same thing as nl2br - but since I do everything in DOMDocument I have to do it myself (create text nodes for each array element and create break nodes between them).

If I'm going to a pre field or back to a text area (IE to edit the data) then I don't have to do anything.

I do NOT recommend running nl2br before it goes into the database - as that means you may have to convert the breaks when re-using the data elsewhere, and the proper way to make a break depends upon the output type (IE <br> for html or <br /> for xhtml, etc.) - so keep the newline character in the database and convert to what you need when you call the data from the database.

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to