在textarea中换行 [英] line breaks in a textarea

查看:128
本文介绍了在textarea中换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道当保存textarea时,你可以使用nl2br()或str_replace将/ n更改为br标签等等。但是,我不知道如何将换行符插入到textarea中。我似乎无法找到很多关于如何将数据放回到带有换行符的textarea中。



例如,我有一个表单,用户可以更新字段。所以用户可以输入:

  foo 
bar
baz

当保存到数据库时,它将被保存为:

  foo< br /> bar< br /> baz< br /> 

现在,当用户在页面刷新后回到该表单时,所有字段都会自动填充以前的数据通过从数据库中获取数据。



然而,textarea将br标签显示为文本而不是在换行符中添加。我也尝试改变br标签,希望textarea能够将这些解释为换行符,而不是喜悦。除此之外,我还尝试转义等。



所以我的问题是可以做到这一点?或者更重要的是可以使用HTML / PHP(即时通讯使用smarty)完成。如果这是不可能的可以使用JavaScript?



例子将不胜感激。



感谢阅读

解决方案

当您将其保存到数据库时,请勿执行 nl2br 在HTML中显示文本时,请执行 nl2br 。我强烈建议不要在数据库中存储任何HTML格式(除非您使用的是丰富的HTML编辑器,在这种情况下,它将很愚蠢)。



换行符 \\\
将会成为textarea中的换行符。


I know when saving a textarea you can use the nl2br() or str_replace to change the /n to br tags etc. However what im not sure about how to insert line breaks into a textarea. I cant seem to find much about putting the data back into a textarea with those line breaks.

For example I have a form where users can update fields. So the user may enter:

foo
bar
baz

When that is saved to the database it would be saved as:

foo<br />bar<br />baz<br />

Now when that user goes back to that form after a page refresh all the fields are automatically populated with their previous data by taking the data from the database.

However the textarea shows the br tags as text instead of adding in the line breaks. i also tried changing the br tags to /n hoping the textarea would interpret these as line breaks but no joy. As well as this I also tried escaping etc.

So my questions are can this be done? Or more importantly can it be done using HTML/PHP (im using smarty). If that isnt possible can it be done using javascript?

Examples would be appreciated.

thanks for reading

解决方案

Don't do nl2br when you save it to the database. Do nl2br when you're displaying the text in HTML. I can strongly recommend to not store any HTML formatting in the database (unless you're using a rich HTML editor as well, in which case it would be silly not to).

A newline \n will just become a newline in the textarea.

这篇关于在textarea中换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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