Okay, so I know to use nl2br() when displaying text that has carriage returns or line breaks (\r,
) in html, but when pulling from the DB and inserting the text into a textarea tag, the breaks and returns aren’t being displayed. I know they’re there, as I’ve not replaced them in any form. The only thing I worry about is that I have stripslashes() taking the escape character out, and it may be destroying the breaks. But it never displays an extra ‘n’ character at the end of what should be the paragraphs.
What should be:
This is some sample text. And here comes a couple line breaks…
And some more text here.
becomes:
This is some sample text. And here comes a couple line breaks…And some more text here.
Why?
For the rest, it’s hard to say (for me at least) without some code.
I’m assuming you’re putting the data back into the textarea, and getting <br>'s in it rather than line breaks.
What you need to do is convert your BR’s back into Line Breaks.
Do not use nl2br for text area. Data will be displayed in the same text form it was saved in the database.
br2nl() ?
The textarea should display the <br />'s that were inserted (not as breaks but as actual html). If you convert from nl to br when inserting into the db, you will need to convert br to nl when you want to put it all back in the textarea.
I apologize, I should have been a bit more clear on things…
On the site I’ve built, the client can go into an admin section, enter text in a textarea (including HTML markup), and have it display in the public facing pages.
When the admin goes to those admin pages, a query is run, retrieving any text that has been previously entered for that page, and places the text in a textarea for the admin to edit. In this portion of the script, there is no use of nl2br().
and \r are supposed to be left intact. I was under the impression that by leaving them intact, when editing the text, they would actually display line breaks in the text. As it is, it seems that the whitespace where the break is is being stripped.
Now, on the public facing pages, the same query pulls the text from the DB, I add nl2br() there to replace the returns and line breaks, and the text is displayed perfectly in the page.
So, I know the
is still there, and being successfully converted to <br />. It just refuses to show in the editable textarea.
Sorry for the double post, but I got the situation resolved. I got it working by going ahead and adding nl2br() before the text was inserted to the DB. Then I wrote a function:
function decode_message($message)
{
$message = str_replace('<br />', "\
\\r", $message);
return $message;
}
to replace the html break with carriage returns, then applied stripslashes() afterword to take care of the quotes.