PHP 长文本显示为用户编写的内容
我需要知道如何以写入的方式显示放入数据库长文本字段中的信息。
就像用户在下面这样写:
My life is full of love I fly like
wedding doves I blow passed stop signs
That intersect with hate lines
-
I know what I am
I know who I be
If you can't accept me
Then don't friend me
我希望它显示数据库中的文本,就像用户在文本区域中写的那样,而不是像这样显示:
我的生活充满了爱 我像婚礼鸽子一样飞翔 我吹过停车标志 与仇恨线相交 我知道我是谁 我知道我是谁 如果你不能接受我 那么就别加我为好友 我
怎样才能在 PHP 中使用 PHP 正确显示信息?
I need to know how to display information put in a database longtext field the way it was written.
Like if a user writes in this below:
My life is full of love I fly like
wedding doves I blow passed stop signs
That intersect with hate lines
-
I know what I am
I know who I be
If you can't accept me
Then don't friend me
I want it to display the text from the database just like the user wrote it in the textarea instead of it displaying in one like this:
My life is full of love I fly like wedding doves I blow passed stop signs That intersect with hate lines I know what I am I know who I be If you can't accept me Then don't friend me
How can I could it in PHP to display the information properly using PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
nl2br()
。它将换行符(例如,从文本区域)转换为 HTML
标记。Use
nl2br()
. It converts line breaks (e.g. from textareas) into HTML<br />
tags.如果您在数据上使用 nl2br,它的显示格式应与原始数据中的格式大致相同文本区域。
但是,我真的建议您阅读有关处理 SQL 注入 和 转义输出等,因为如果你不小心的话,你很可能会遇到一些跨站脚本(XSS)问题。
If you use nl2br on the data, it should appear with approximately the same formatting as it did in the originating textarea.
However, I'd really recommend reading about things like handling SQL injection and escaping output, etc. first as you're quite possibly setting yourself up for some cross site scripting (XSS) issues if you're not careful.
如果要显示预先格式化的文本,请将其括在
标签中
许多人不喜欢
大多数浏览器呈现的预格式化文本与普通文本不同,因此您可能需要调整样式表
If you want to display a preformated text enclose it in
<pre></pre>
tagsMany people dislike the
<pre>
element because it is often abused by bad developers and designersbut this is exactly the case it is supposed to be used for.
Most browsers render preformated text different then normal text, so you might have to adjust your stylesheet
我怎么强调都不为过:您永远不应该按原样输出用户生成的内容,但要小心转义输出的所有文本(请参阅:http://php.net/manual/en/function.htmlspecialchars.php )。
I can't stress it enough: You should never output user-generated content as it is but be careful that you escape all text you output (see: http://php.net/manual/en/function.htmlspecialchars.php ).