How to save a new line between textarea and database?

I save user input in the text area to the database using nl2br().<br/> Now the problem is that I do not want to show the "br" tags when I show this input in the text area later, but as a new line. <br/> I used str_replace , but this seems to add a new line every time I go back and forth. eg. user enters

 Hello World 

It is stored as

 Hello<br />World 

excellent with me. but when I show it again in the text area, I get (after str_replace..in br tags with "\ n")

 Hello World //which if i submit gives me Hello<br /><br />World 

Is there any way to achieve what I want to do here?

+4
source share
2 answers

Do not store it with nl2br . You should only store raw data. If you want to update it to display, use nl2br at this time.

 if ($storing) { $dbstmt->execute($_POST['textarea-value']); } else { $textareaValue = $db->query($select); echo "<div>" . nl2br(htmlspecialchars($textareaValue)) . "</div>"; echo "<textarea>" . htmlspecialchars($textareaValue) . "</textarea>"; } 
+1
source

The nl2br function adds <br/> after each line break character ( \r\n, \n\r, \n and \r ).

However, it does not remove the break line. As a result, when you use str_replace to add a line break, it appears as double.

There are two options: first, change str_replace to just delete <br/> without adding a new line character or, secondly, not use nl2br .

I would choose the second one and use nl2br when I need to display data inside html.

+1
source

Source: https://habr.com/ru/post/1490701/


All Articles