PHP 长文本显示为用户编写的内容

发布于 2024-10-09 21:13:21 字数 461 浏览 7 评论 0原文

我需要知道如何以写入的方式显示放入数据库长文本字段中的信息。

就像用户在下面这样写:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

俯瞰星空 2024-10-16 21:13:21

使用nl2br()。它将换行符(例如,从文本区域)转换为 HTML
标记。

Use nl2br(). It converts line breaks (e.g. from textareas) into HTML <br /> tags.

少女的英雄梦 2024-10-16 21:13:21

如果您在数据上使用 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.

久夏青 2024-10-16 21:13:21

如果要显示预先格式化的文本,请将其括在

标签中

  <pre>...your text here...</pre>

许多人不喜欢

 元素,因为它经常被滥用糟糕的开发商和设计师
但这正是它应该用于的情况。

大多数浏览器呈现的预格式化文本与普通文本不同,因此您可能需要调整样式表

body, pre {font-family: ...;font-size:....}

If you want to display a preformated text enclose it in <pre></pre> tags

  <pre>...your text here...</pre>

Many people dislike the <pre> element because it is often abused by bad developers and designers
but 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

body, pre {font-family: ...;font-size:....}
五里雾 2024-10-16 21:13:21

我怎么强调都不为过:您永远不应该按原样输出用户生成的内容,但要小心转义输出的所有文本(请参阅: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 ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文