PHP 回显 HTML 标签

发布于 2024-12-19 22:24:47 字数 953 浏览 0 评论 0原文

我遇到一些文本区域问题。我使用像 My php 文件这样的文本区域将文本插入到数据库中

<textarea id="e1" name="content"></textarea>

,然后如图所示在其上运行一些内容,以使用 \n 和 \r: 制作 HTML 标记

$str     = $_POST['content'];
$order   = array("\r\n", "\n", "\r");
$replace = '<br />';
$content = mysql_real_escape_string(str_replace($order, $replace, $str));

,并将结果插入到数据库中。 然后,我在 $content 中调用数据库的结果,并将其放入文本区域,如下所示:

<textarea id="e1" name="content"><?php echo $content ?></textarea>

当我查看该页面时,
标记可见。
一个例子:
我将其提交到文本区域,

Hello,
This is text.
Best Rergards,
Testificus

它由我的 php 代码处理,然后回显到文本区域。当它回显到文本区域时,它会以以下形式出现:

Hello,<br />This is text.<br />Best Regards,<br />Testificus

是否有任何方法可以使文本看起来像以前那样,使用
标签进行操作,而不是呈现为文本?感谢您的帮助,如果不清楚我的意思,请告诉我。

I am having a problem with some textarea things. I insert text into my database using a textarea like

<textarea id="e1" name="content"></textarea>

My php file then runs some stuff on it as shown to make HTML tags out of \n and \r:

$str     = $_POST['content'];
$order   = array("\r\n", "\n", "\r");
$replace = '<br />';
$content = mysql_real_escape_string(str_replace($order, $replace, $str));

and the result gets inserted into a database.
I then call the result from the database in $content and put that in a text area like so:

<textarea id="e1" name="content"><?php echo $content ?></textarea>

When I view that page the <br /> tags are visible.

An Example:

I submit this into the text area

Hello,
This is text.
Best Rergards,
Testificus

It gets processed by my php code and then is echoed into the textarea. When it is echoed into the textarea it comes out in the form:

Hello,<br />This is text.<br />Best Regards,<br />Testificus

Is there any way of having the text look like before with the <br /> tags in action rather that being presented as text? Thanks for your help and let me know if it is not clear what I mean.

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

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

发布评论

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

评论(4

孤独岁月 2024-12-26 22:24:47

您可以将 \r\n 保留在数据库中,然后当您想要输出为 html 时,只需通过 nl2br() 运行文本即可将新行交换为
元素。如果您这样做,则无需担心额外的转换,只需在需要时执行一次。

You can leave the \r\n's in the database and then when you want to output to html, simply run the text through nl2br() which swaps new lines for <br> elements. If you do it this way you don't need to worry about extra converting, it is done only once when you need it.

梦醒时光 2024-12-26 22:24:47

我说得对吗,您想用
标记替换换行符吗?为什么不直接使用nl2br()

如果您想继续使用您的示例,则必须使用:

$replace = "
gt;\r\n";

Am I right, you want to replace the newlines with the <br /> tag? Why don't you just use nl2br().

If you want to keep using your example, you have to use:

$replace = "<br />\r\n";

耀眼的星火 2024-12-26 22:24:47

我不确定它有多有效,但


 

代替换行符应该有效。

I'm not sure how valid it is but


 

in place of the newlines should work.

反话 2024-12-26 22:24:47

将您的代码替换

$str     = $_POST['content'];
$order   = array("\r\n", "\n", "\r");
$replace = '<br />';
$content = mysql_real_escape_string(str_replace($order, $replace, $str));

为以下代码:

$str     = $_POST['content'];
$content = mysql_real_escape_string(nl2br($str));

Replace your code

$str     = $_POST['content'];
$order   = array("\r\n", "\n", "\r");
$replace = '<br />';
$content = mysql_real_escape_string(str_replace($order, $replace, $str));

with this code :

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