Help with rn (/r/n) displaying in textarea

Recommended Posts

I have a textarea displaying data from a mysql table. However, a user discovered today that if they use a carriage return in the textarea, the data comes out with "rn" at the location in the text where the carriage return goes. I have been searching the forums for a while this afternoon and have found quite a few references to this, but have been unable to fix my problem.

I am using mysql_real_escape_string before inserting into database (I am not using anything else at insertion time). Is there something else that I should be using at either the insertion into the database or when I display the data? I have tried various combinations of htmlspecialchars() and htmlentities() and nl2br() with no luck.

Here is a sample of the output in my textarea:

copy diagonal lines and a square with no more than 1/4" overlap or gap at point of closure(modified).rn2.Patient will draw a person ........

Should look like:

copy diagonal lines and a square with no more than 1/4" overlap or gap at point of closure(modified).

Share this post

Link to post

Share on other sites

Why are you stripping slashes for output? So you are aware, It is \r\n, the scape for carriage return and newline. If you strip the slashes, it will NOT parse as a new line and will simply display as 'rn'

Share this post

Link to post

Share on other sites

carriage returns are not preserved when sent via form to mysql db, therefore, nl2br is required.

what is happening when you use mysql_real_escape_string and nl2br together?

try:

$stg = mysql_real_escape_string (nl2br ($_POST['']));

you were overwriting the $stg variable the way you were doing it.

when I try that I get the following output in the textarea:

1.rn2.rn3.rn

Where it should be:

1.

2.

3.

Why are you stripping slashes for output? So you are aware, It is \r\n, the scape for carriage return and newline. If you strip the slashes, it will NOT parse as a new line and will simply display as 'rn'

I was stripping the slashes because I had an earlier problem where it was displaying slashes everywhere I had ' or " .

Share this post

Link to post

Share on other sites

That doesn't make much sense, newlines should be invisible. How are they being created?

I am not creating them on purpose. The users are trying to create a numbered list by simply pressing the "Enter" key on the keyboard, something that I didn't plan for when I wrote the app. If I knew that the numbered list would be the same every time, I would accomodate it with multiple fields, but that is not an option for this application.

Share this post

Link to post

Share on other sites

I had exactly the same issue before. When using mysql_real_escape_string from posted data, then using that to populate a textarea (say if there were an error), all the new lines would be displayed as (\r\n).

The problem is, you shouldn't be doing mysql_real_escape string until the data is being inserted into the database. So if you have to post this back to the page, use striptags or clean it some other way. Try to use the $_POST array instead of running it through loads of functions.

Then, when the data is to be inserted into the database, you can properly escape it.

Share this post

Link to post

Share on other sites

This problem because of two php functions conflicting. If "magic_quotes_gpc" is enables (assuming that you are using php version less than 5.3.0) then you should not use mysql_real_esacpe_string when inserting data into database.