【问题标题】:Repopulate nl2br saved content inside textarea without <br> tags?在没有 <br> 标签的情况下在 textarea 中重新填充 nl2br 保存的内容?
【发布时间】:2012-08-29 02:16:25
【问题描述】:

当用户从文本区域保存文本时:

bfajsdb fkjasdfasjkdfasdfasdf asdf asdf sdf

asdfasdfasdfasdf asd asdf asdf 

sdfasdfasdf

在 codeignter 中使用 nl2br_except_pre 函数从 textarea 将其保存在数据库中,如下所示:

bfajsdb fkjasdfasjkdfasdfasdf asdf asdf sdf
<br>
asdfasdfasdfasdf asd asdf asdf 
<br>
sdfasdfasdf

在 div 或 p 标签中输出很好。或一般在 html 输出中。

但是,当再次在 textarea 中重新填充时,它会显示
标签,这很丑陋并且会使用户感到困惑。

如何在从数据库重新填充保存的值之前清理输出,使其在文本区域中看起来像这样:

bfajsdb fkjasdfasjkdfasdfasdf asdf asdf sdf

asdfasdfasdfasdf asd asdf asdf 

sdfasdfasdf

不是这样的:

bfajsdb fkjasdfasjkdfasdfasdf asdf asdf sdf
<br>
asdfasdfasdfasdf asd asdf asdf 
<br>
sdfasdfasdf

【问题讨论】:

  • 那么不要在你的数据库中存储 HTML :)
  • 我需要它,因为我在页面上的其他 html 内容中使用它。我需要一种方法来清理或更改输出,例如$row->textarea_content 删除 br 标记,但要正确创建新行。
  • 然后在渲染时应用 nl2br() :)
  • 喜欢:nl2br($row->textarea_content); ?我会试试的
  • 什么都不做 nl2br($row->textarea_content);

标签: php nl2br


【解决方案1】:

好吧,粗略的回答是“只使用strip_tags”:

$raw_textarea = 'bfajsdb fkjasdfasjkdfasdfasdf asdf asdf sdf
<br>
asdfasdfasdfasdf asd asdf asdf 
<br>
sdfasdfasdf';

$clean_textarea = strip_tags($raw_textarea);
echo $clean_textarea;

但是,事实上,还有更多:也许您需要担心清理您的输出,而不仅仅是清除不相关的标签。我建议阅读 this discussion 关于该主题的内容。

【讨论】:

  • 哇,看起来很有效,而且很简单,谢谢。在我的情况下,只允许使用 br 标签,所以没有粗体文​​本、列表等。
【解决方案2】:

不要在数据库中保存 nl2br() 数据;您的目标应该是在数据库中存储数据而不是 html。

在必要时在视图中呈现时将 nl2br() 应用于您的数据库值。顺便说一句,Textarea 字段不需要 nl2br() 处理。

当然,当你输出它时,你也应该在 nl2br() 之前应用 htmlspecialchars()。

【讨论】:

    【解决方案3】:

    试试nl2br php函数

    它会产生你想要的输出。

    是的,这是正确的。如果您在显示数据时使用 nl2br 函数,它将起作用。

    【讨论】:

    • 是的,这是正确的。如果您在显示数据时使用 nl2br 函数,它将起作用。
    猜你喜欢
    • 2014-10-08
    • 2015-12-02
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多