PHP 回显 HTML 标签
我遇到一些文本区域问题。我使用像 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将
\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 throughnl2br()
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.我说得对吗,您想用
标记替换换行符吗?为什么不直接使用nl2br()
。如果您想继续使用您的示例,则必须使用:
$replace = "
gt;\r\n";
Am I right, you want to replace the newlines with the
<br />
tag? Why don't you just usenl2br()
.If you want to keep using your example, you have to use:
$replace = "<br />\r\n";
我不确定它有多有效,但
代替换行符应该有效。
I'm not sure how valid it is but
in place of the newlines should work.
将您的代码替换
为以下代码:
Replace your code
with this code :