3

I am collecting text from a textarea. The text has line breaks in it. I then save it to a database.

However when I retrieve it from the database and display it back in a textarea for editing, the line breaks are gone and the text turns into one long run-on string.

I would like the line breaks originally there to reappear. How can I make the line breaks appear as they should when inserted back into a textarea?

Note: I cannot just convert them to a <br> because this is going inside a textarea where <br> elements would not get rendered, but just written out as text. I want the actual line breaks.

6
  • can't you use string replace function to replace \n with <br/> ? Or have I not gotten your question clearly Commented Nov 17, 2012 at 23:39
  • Eritrea, there should be no reason the OP has to do something like that. MySQL saves \n just fine on its own. Commented Nov 17, 2012 at 23:40
  • If you view the source of the page, are the lines in the edit field split up correctly? Commented Nov 17, 2012 at 23:41
  • post your query so and your php code which is executing that query, without it there is no way we can tell why is your \n disappearing Commented Nov 17, 2012 at 23:59
  • Sorry if I did not phrase question correctly. I want it to preserve the line breaks when you display it in an open textarea box, not in html. In other words: <textarea>Some text here</textarea> Within the textarea tags, any tags show up as <br> not as new lines. New lines are invisible within the textarea but they seem to go away when you re-display out of the database. Commented Nov 18, 2012 at 4:01

4 Answers 4

5

Everyone is suggesting to convert the line breaks to <br />, however as you stated in your question that's not what you want. You want to display the line breaks in the textarea again. So, what you need here is stripcslashes() - this will strip away \n and give you what you're after, being line breaks in your textarea, not html <br /> tags in the textarea:

PHP: stripcslashes

Usage:

echo stripcslashes($input_text);
Sign up to request clarification or add additional context in comments.

Comments

0

You coud use something like this:

echo nl2br($someText);

Comments

-1

This is what I usually do when dealing with this kind of problem:

  1. Save whatever data receiver in form. This mean, if user use newline, store it that way. Don't change it.
  2. Upon displaying data, convert every newline to <br />:

    echo str_replace("\n", "<br />", $data);
    

Using this technique, you can preserve whatever formatting data used by your user in textarea.

4 Comments

Can't use <br> as it displays as tag in textarea box. I am going to try saving the \n coming in as something else like *** and when I display in textara, use your suggestion to replace the *** with \n and see if that works.
My approach did not work. Nothing printed at all. So still looking for answer.
here's the deal: usually, user will use [return] as new line. This translated as \n. Leave this as-is and store it to database. Next time user want to change the content, just show them whatever data is saved (this include the \n). However, when you want to show the data to html, format them first, by replacing \n with <br />.
If anyone could tell me how to display a return in a textarea, that would solve the problem. Have tried \n, '\n', "\n", and same variations with \r\n and they just show up as characters in the text rather than as returns.
-1

str_replace() doesn't go like this:

echo str_replace($data, "\n", "<br />");

It goes like this:

str_replace("SEARCH_FOR","REPLACE_WITH",STRING);

In the sense that SEARCH_FOR is what you are trying to find, REPLACE_WITH is what you want to replace with, and STRING is the variable you are using (such as $data).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.