Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save Textarea input after convert line breaks as BR in SQL

Tags:

sql

php

textarea

I use ckeditor in admin panel but in user submit form use simple textbox so user can input text and submit. Problem is when user enter text in textarea with Line Breaks it saves as it in SQL. I want to add BR after each line in sql.

For Example User Submits:

    ![F.R.I.E.N.D.S.:
(F)ight for you.
(R)espect you.
(I)nvolve you.
(E)ncourage you.
(N)eed you.
(D)eserve you and
(S)tand by you.][1]![SCREENSHOT oF DB SAVE][2]

got saved in DB as it with next line showing in output. But I want to save in DB as:

    F.R.I.E.N.D.S.:<br />
(F)ight for you.<br />
(R)espect you.<br />
(I)nvolve you.<br />
(E)ncourage you.<br />
(N)eed you.<br />
(D)eserve you and<br />
(S)tand by you.

I use nl2br but its not working on user submit form If I use nl2br on admin processing form then on those fields already added with ckeditor it adds two BR tags.

Code used on user submit form is:

<textarea name="content" id="content" cols="60" rows="10" class="span7"><?php if(isset($content)) { echo $content; } ?></textarea>

$content = trim($_POST["content"])
$content = mysql_real_escape_string($content);
$content = nl2br($content);

No processing is used on admin approval form where ckeditor used on textarea. Text output from DB appears without Line Breaks in a single line in ckeditor. if I use nl2br while output on this form it works but adds double BRs on earlier text posted through ckeditor.

also tried $content = preg_replace("/\r\n|\r/", "<br />", $content); as suggested by some one on stackoverflow on similar question

pls suggest me some function for this problem.

also suggest If I need to use some function like htmlentities or stripslashes to process content before Inserting into SQL.

like image 293
Vehlad Avatar asked Dec 03 '22 01:12

Vehlad


1 Answers

Just replace the new line \r\n, \r first, then trim it.

$content = preg_replace("/\r\n|\r/", "<br />", $_POST["content"]);
$content = trim($content])

Or:

$content = nl2br($_POST["content"]);
$content = trim($content)

Good luck.

like image 161
lijinma Avatar answered Jan 29 '23 11:01

lijinma