问题描述
我有一个多行文本框。当用户输入时,文本框会将文本包装起来,并保存为一行。用户可能会输入换行符,例如当输入一个项目符号列表时:
这里有一些建议:
- 修复此
- 移除
- 另一件事
现在,当我尝试显示此字段的值时,就会出现问题。为了保持格式化,我现在将演示文稿包装在< pre>
中 - 这可以保留用户提供的中断,但是当有大量文本保存为单行,它将整个文本块显示为单行,导致需要水平滚动来查看所有内容。
是否有一种优雅的方式来处理这两种情况?
处理这个问题的最简单方法是将所有换行符 \\\
转换为
< br>
换行符。以PHP为例,这是通过使用 nl2br()
函数完成的。
如果你想要一些更加奇特的东西 - 比如你引用的列表被转换成一个实际的HTML < ul> $ c $例如 - 你可以考虑一个简单的语言,比如SO使用的。它带有自然而简单的规则,如
#标题1
##标题2
###标题3
*无序列表项目
*无序列表项目
1.编号列表项目
2.编号列表项目
code>
etc ....
I have a multi-line text box. When users simply type away, the text box wraps the text, and it's saved as a single line. It's also possible that users may enter line breaks, for example when entering a "bulleted" lists like:
Here are some suggestions:
- fix this
- remove that
- and another thing
Now, the problem occurs when I try to display the value of this field. In order to preserve the formatting, I currently wrap the presentation in <pre>
- this works to preserve user-supplied breaks, but when there's a lot of text saved as a single line, it displays the whole text block as single line, resulting in horizontal scrolling being needed to see everything.
Is there a graceful way to handle both of these cases?
The easiest way of dealing with this is turning all line breaks \n
into <br>
line breaks. In PHP for example, this is done using the nl2br()
function.
If you want something a bit more fancy - like the list you quote getting converted into an actual HTML <ul>
for example - you could consider a simple "language" like Markdown that SO uses. It comes with natural, simple rules like
# Heading 1
## Heading 2
### Heading 3
* Unordered List item
* Unordered List item
1. Numbered List item
2. Numbered List item
etc....
这篇关于最佳做法:显示通过多行文本框输入的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!