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

问题描述

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



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

  

当您将其保存到数据库时,请勿执行 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中换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 08:33