为了账号安全,请及时绑定邮箱和手机立即绑定

使用 PHP nl2br 在 HTML 页面中显示 HTML 代码

使用 PHP nl2br 在 HTML 页面中显示 HTML 代码

PHP
慕尼黑5688855 2022-09-03 16:35:00
请帮助尝试所有方法,没有任何效果。我正在使用PHP 7向MySQL DB插入一些代码,然后插入代码,这一切都很好;因此:Eg:<p>Hello:<b>Amber</b>\r\n<div>\r\n<hr>\r\n\r\n<button onclick=\"insertTextAtCursor()\">Lisa sisend</button>当我在HTML中使用PHP调用记录集时,由于HTML代码被隐藏,因此不会显示任何内容。我需要以纯文本形式显示HTML代码,但“不显示\r\n显示换行符” - 格式化;因此:Eg:<p>Hello:<b>Amber</b><div><hr><button onclick=\"insertTextAtCursor()\">Lisa sisend</button>纯css和javascrpt代码在这个PHP行中很好地显示出来,但不是HTML:$author_code1 = null;$author_code1 = $row["author_code1"];  $author_code1 = str_replace('\r\n', '<br />', $row['author_code1']);echo $author_code1;    更接近它,因此添加:<?phpecho "<xmp>";echo str_replace("\r\n", "<br/>", $author_code1);echo "</xmp>";?>  这在一行中显示所有内容:</html>\r\n </textarea>\r\n</div>\r\n   </div>\r\n</section>\r\n<button>Lisa send</button>    我的问题是为什么它没有突破到下一行?喜欢:<div><p>Hello:<b>Amber</b></p></div><hr>    <button>Lisa send</button>请帮忙。
查看完整描述

1 回答

?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

我认为它在很大程度上提供了答案。许多人只是回复,有些复制粘贴。但是,我找到了一个解决方案:


$author_code1 = null;

$author_code1 = $row["author_code1"];  


$author_code1 = str_replace('\r\n','', $author_code1);

$author_code1 = str_replace('<',"&lt;",     $author_code1);

$author_code1 = str_replace('>',"&gt;",     $author_code1);

$author_code1 = str_replace('</',"&lt;/",   $author_code1);



<pre>

 <code class="html hljs">


<!-- my db code -->

   <?php echo $author_code1; ?>

<!-- my db code -->


 </code>

</pre>


查看完整回答
反对 回复 2022-09-03
  • 1 回答
  • 0 关注
  • 91 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号