How to save line breaks from a textarea to the database?

Asked

Viewed 5,196 times

8

I have a textarea and would like to save the user-made line breaks in the database. For example, user writes the following:

"Lorem Ipsum.
Is simply.
Dummy text.
Of the printing.
And typesetting."

But in the database, it is saved as:

"Lorem Ipsum. Is simply. Dummy text. Of the printing. And typesetting."

I would like it to be written with the breaks of lines made in the textarea. How can I do it using PHP?

  • Line breaks were refused. But in the case, it would skip the line to each point in the example.

  • 1

    @Antonioalexandre that you said there is nonsense. It is no problem to record line break in VARCHAR. What changes in the text formats for the end user is basically the storage capacity.

  • The following test shows that Mysql respects the breaks when saving: http://sqlfiddle.com/#! 9/b8938a/1

  • @Marcelo the problem may not be in the save, but in the fact that you don’t switch to <br> when playing back on screen. You need to test this. If this is the case, you save in DB, but at the time of RECOVER uses the nl2br like Miguel said.

  • @Bacco, I checked here and really confused with the input fields that always saved as varchar and textarea that always saved as text. Really varchar accepts yes line break n and said nonsense when he said he did not accept. In input type="text" fields you cannot type line break, but in the database you enter line break even if you are varchar. However insert <br> to break line in the bank is not good practice and in that I was right. Unless it is purposeful as in editing a field used in WYSIWYG editor.

4 answers

6

You can use nl2br, to test you can do:

<form method="POST">
    <textarea name="test"></textarea>
    <input type="submit">
</form>
<?php
echo nl2br($_POST['test']);

Will give a Warning before the first try because you haven’t done a post yet but after you submit it will already be ok.

  • It Works! And I confess that I did not know this function.

  • It’s not my question, @miguel. But I tested it because I didn’t know the function ;)

  • Ah, and the edition I suggested was just to avoid the failure of $_POST unfinished ;)

  • Thank you very much! It worked perfectly!

  • 1

    @Marcelo if helped you should accept the answer. Below the top/bottom left of the answer

  • No user nl2br to save to database. Use field type as text in database.

  • @Antonioalexandre the field text does not cause line breaks to continue: http://stackoverflow.com/questions/498461/how-to-save-user-entered-line-breaks-from-a-textarea-to-a-database, http://stackoverflow.com/questions/12729524/php-mysql-storingline-breaks-in-text-area-in-databasedatabase

  • @Miguel, line breaks remain if the field type in the database is text. It does not appear in html pq the html strip, but if you look at the source code you notice the line breaks. But to display on the screen you must use this function yes. To save with this, use only if you are only working with html itself, like who saves content from a WYSIWYG editor. For example: If I save with <br> and then want the content to use in a file q is not html, it will get <br> polluting the text.

  • @Antonioalexandre ok, I did not know this but until today I always did so to save in DB, whether field text or not. Obgado because I really did not know

  • @Miguel, this is it, this is a community and everyone is here to help themselves. As you are a reputable user, increase your response to help more people who visit in the future.

  • 1

    Miguel that @Antonioalexandre is talking about is bullshit, TEXT has nothing different from VARCHAR or CHAR in relation to line breaks.

  • 1

    @Bacco confessed that I also found it unlikely but he spoke with such certainty that since he had not yet tested and not 100% sure that would not work, who was I to disagree?! But obgado you saved me a few minutes of tests

  • 1

    @Miguel always do the tests :)

  • @Bacco didn’t do it because I don’t have access to a comp right now, but since you already told me I think it’s unnecessary/irrelevant to take this test

  • The following test shows the breaks saved in the correct places ;) http://sqlfiddle.com/#! 9/b8938a/1

  • @Bacco obgado, exact but then you have entered them directly in the query, so that they reach there, when passing through the server until the insertion we have to keep using my correct answer?

  • 1

    @Miguel, I mentioned this in the question. I put it in the query just to show that it was saved, because SQL Fiddle doesn’t show the break right either :) In the case of the author of the question nl2br at the time of the display is the likely solution even. The only thing he needs to see is if he captured the broken string, but then just testing. I only recommend not applying nl2br when saving, but only when displaying. Your answer seems to me correct, just missing the author have posted his code to be able to give more details.

  • @ Bacco, I may have confused myself with something I said, actually with conviction now I can only say that the input type text that does not save line breaks and that is obvious. But I’ll be auditioning on Monday when I get home. But what I said most strongly is that using nl2br before saving to the database is bad practice, unless you’re using a WYSIWYG and your goal is to actually save html to the database. And you just agreed with me.

Show 13 more comments

2

You can use the function nl2br():

Ex.:

echo nl2br("Essa\r\neh\n\ruma\nstring\r");

Output:

Essa<br />
eh<br />
uma<br />
string<br />

Or put the text to be saved between tags <pre></pre>.

Ex.:

<pre>
Linha 1.
           Linha 2 esta a direita da linha 1.
           Linha 3 esta alinhada com a linha 2.
</pre>

See more in W3C Wiki - HTML/Elements/pre

1

Do not use nl2br to save to the bank so as not to dirty the bank with html. nl2br you can use at the time it will display on the screen. Except if your intention is to save html in the database, for example, if saving the content of a WYSIWYG editor.

-2

I know it may not be within the scope of the question, but it is also a way to solve your problem and still implement a more elegant solution.

Use a Texteditor.

In addition to giving you better security, it also allows you to customize the text. See that Stackoverflow itself uses Texteditor.

There are several types of texteditor on the market, see which one suits your solution best and implement. It may be a bit tricky at first, but you will see that the final solution will be much more elegant.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.