如何在SQL中将换行换行符保存为BR后保存Textarea输入

Veh*_*lad 4 php sql textarea

我在管理面板中使用ckeditor但在用户提交表单中使用简单的文本框,因此用户可以输入文本并提交.问题是当用户使用换行符在textarea中输入文本时,它会在SQL中保存.我想在sql中的每一行之后添加BR.

例如用户提交:

    ![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]
Run Code Online (Sandbox Code Playgroud)

保存在DB中,下一行显示在输出中.但我想在DB中保存为:

    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.
Run Code Online (Sandbox Code Playgroud)

我使用nl2br,但它不能用于提交表单如果我在管理处理表单上使用nl2br,那么在那些已经添加了ckeditor的字段中,它会添加两个BR标记.

用户提交表单上使用的代码是:

<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);
Run Code Online (Sandbox Code Playgroud)

在textarea上使用ckeditor的管理员批准表单上不使用任何处理.DB中的文本输出在ckeditor中的单行中没有换行符.如果我在这个表单上输出时使用nl2br它可以工作但是在通过ckeditor发布的早期文本上添加了双BR.

也尝试$content = preg_replace("/\r\n|\r/", "<br />", $content);了一些关于stackoverflow在类似问题上的建议

请告诉我这个问题的一些功能.

还建议如果我需要在插入SQL之前使用某些函数(如htmlentities或stripslashes)来处理内容.

lij*_*nma 11

只需先更换新线\r\n, \r,然后修剪它.

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

要么:

$content = nl2br($_POST["content"]);
$content = trim($content)
Run Code Online (Sandbox Code Playgroud)

祝好运.

  • :)我很高兴,在trim()之后使用mysql_real_escape_string(),祝你好运:) (2认同)

blu*_*112 6

您需要使用nl2br在需要时显示值,而不是保存它.