PHP 保存用户输入的内容

发布于 2024-08-03 10:27:06 字数 313 浏览 3 评论 0原文

如何在数据库中保存用户给出的“输入”并将其显示给其他用户?

我存储用户的问题并使用以下函数来清理用户数据,准备它,并分别执行SQL命令。

 pg_escape_string
 pg_prepare
 pg_execute

我使用 htmlentitiesENT_QUOTES 来转换数据 HTML。 此过程删除问题中的所有输入(显然以 \n 形式)。

我想要一个与 SO: 类似的问题系统:仅向用户显示双输入作为换行符。

How can you preserve "enters" given by the user in the database and show them then to other users?

I store the question of the user and use the following functions to sanitize the user data, prepare it, and execute the SQL command, respectively.

 pg_escape_string
 pg_prepare
 pg_execute

I use htmlentities with ENT_QUOTES to convert the data HTML.
This procedure removes all enters, apparently in the form \n, in the question.

I would like to have a similar question-system as in SO: to show only double enters to users as line breaks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

恍梦境° 2024-08-10 10:27:06

调用 htmlentities() 后,调用 nl2br()。 Web 浏览器会忽略普通的换行符,因此您需要将它们转换为
元素才能显示它们。

nl2br — 在字符串中的所有换行符之前插入 HTML 换行符

描述

string nl2br ( string $string [, bool $is_xhtml= true ] )
返回在所有换行符之前插入

的字符串。

例如:

echo nl2br(htmlentities($input));

要仅显示双换行符并忽略单个换行符,您可以使用更复杂的字符串替换函数, preg_replace

echo preg_replace('/\n\s*\n/', "<br />\n<br />\n", htmlentities($input));

这里 '/\n\s*\n/' 匹配一个换行符,后跟任意数量的空格,然后是另一个换行符。它用两个
元素替换任何此类子字符串。单个换行符将被忽略。它也很好,因为它会忽略不可见的无关空格和制表符,例如用户输入以下内容:

这是一个段落。\n
它很短。\n
<空格><制表符>\n
这是另一段。\n
它也很短。

After you call htmlentities(), call nl2br(). The web browser ignores plain newline characters so you need to convert them to <br /> elements to make them show up.

nl2br — Inserts HTML line breaks before all newlines in a string

Description

string nl2br ( string $string [, bool $is_xhtml= true ] )
Returns string with <br /> or <br> inserted before all newlines.

For example:

echo nl2br(htmlentities($input));

To only show double newlines and ignore single ones, you could instead use a more sophisticated string replacement function, preg_replace:

echo preg_replace('/\n\s*\n/', "<br />\n<br />\n", htmlentities($input));

Here '/\n\s*\n/' matches a newline, followed by any amount of whitespace, followed by another newline. It replaces any such substring with two <br /> elements. Single newlines are ignored. It's also nice because it'll ignore extraneous spaces and tabs that are invisible, such as if a user typed this:

This is a paragraph.\n
It is pretty short.\n
<space><tab>\n
Here's another paragraph.\n
It too is short.

佞臣 2024-08-10 10:27:06

PHP 的 nl2br() 函数应该可以解决这个问题,并允许您将 \n 字符转换为
html 标记。

要启用“换行符两次输入”行为,您应该运行正则表达式将每对连续的
标记转换为单个
标记(您也可以在对文本运行 nl2br() 之前使用 \n 字符执行此操作)。

PHP's nl2br() function should do the trick and allow you to convert \n characters to <br> html tags.

To enable the "two enters for a newline" behavior, you should run a regex to turn every pair of consecutive <br> tags into a single <br> tag (you could also do this with \n characters before running nl2br() on the text).

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