如何在SQL中将换行符转换为BR后保存Textarea输入 [英] How to save Textarea input after convert line breaks as BR in SQL

查看:25
本文介绍了如何在SQL中将换行符转换为BR后保存Textarea输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

例如用户提交:

    ![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.

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

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);

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

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.

也尝试过 $content = preg_replace("/\r\n|\r/", "<br/>", $content); 正如有人在 stackoverflow 上建议的那样类似问题

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.

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

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

推荐答案

只需先替换新行 \r\n, \r ,然后修剪即可.

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

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

或者:

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

祝你好运.

这篇关于如何在SQL中将换行符转换为BR后保存Textarea输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆