php 如何使用换行符回显文本区域的输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6344708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to echo an input of an textarea with line breaks?
提问by Muiter
I'm using an textarea to send text to my DB.
我正在使用 textarea 将文本发送到我的数据库。
Screenshot of db:
数据库截图:
When I'm reading from the DB it removes the line breaks, how can I keep them in $row['opmerkingen']?
当我从数据库中读取时,它会删除换行符,如何将它们保存在 $row['opmerkingen'] 中?
回答by binaryLV
When displaying text, use nl2br()
to convert newlines to <br/>
tags, i.e., instead of <?php echo $row['text']; ?>
, use <?php echo nl2br($row['text']); ?>
.
显示文本时,使用nl2br()
将换行符转换为<br/>
标签,即<?php echo $row['text']; ?>
使用代替<?php echo nl2br($row['text']); ?>
。
By default, browsers display newlines as spaces, therefore they have to be converted to <br/>
tags.
默认情况下,浏览器将换行符显示为空格,因此必须将它们转换为<br/>
标签。
For those who find this useful - please consider using white-space: pre-line
, suggested by Emil Vikstr?m. I'm not a web guy anymore and easily can't verify this, but Boaz says in comments that it is supported by all modern browsers. If so, that should be preferred to using nl2br()
.
对于那些觉得这很有用的人 - 请考虑使用white-space: pre-line
,由 Emil Vikstr?m 建议。我不再是一个网络人,很容易无法验证这一点,但 Boaz 在评论中说,所有现代浏览器都支持它。如果是这样,那应该比使用nl2br()
.
回答by Emil Vikstr?m
An alternative to nl2br is to make use of the CSS attribute white-space:
nl2br 的替代方法是使用 CSS 属性white-space:
white-space: pre-line;
回答by Sumith Harshan
I put as follows but not working with single quotes.
我提出如下但不使用单引号。
echo $row['text'].'\n';
Put the double quotes. Then worked.
把双引号。然后工作了。
<textarea rows="10" cols="62" style="white-space: pre-line;" wrap="hard">
<?php echo $row['text']."\n"; ?>
</textarea>
When we getting data it is comming with \r\n. Also use the double quotesthere.
当我们获取数据时,它带有\r\n。在那里也使用双引号。