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

请帮助尝试所有方法,没有任何效果。我正在使用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;    

更接近它,因此添加:


<?php

echo "<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>

请帮忙。


慕尼黑5688855
浏览 92回答 1
1回答

陪伴而非守候

我认为它在很大程度上提供了答案。许多人只是回复,有些复制粘贴。但是,我找到了一个解决方案:$author_code1 = null;$author_code1 = $row["author_code1"];&nbsp;&nbsp;$author_code1 = str_replace('\r\n','', $author_code1);$author_code1 = str_replace('<',"&lt;",&nbsp; &nbsp; &nbsp;$author_code1);$author_code1 = str_replace('>',"&gt;",&nbsp; &nbsp; &nbsp;$author_code1);$author_code1 = str_replace('</',"&lt;/",&nbsp; &nbsp;$author_code1);<pre>&nbsp;<code class="html hljs"><!-- my db code -->&nbsp; &nbsp;<?php echo $author_code1; ?><!-- my db code -->&nbsp;</code></pre>
打开App,查看更多内容
随时随地看视频慕课网APP