Php 带'的双线中断;前';标签和';nl2br';

Php 带'的双线中断;前';标签和';nl2br';,php,html,line-breaks,pre,nl2br,Php,Html,Line Breaks,Pre,Nl2br,我对pre标记使用了nl2br函数,但我遇到了一个奇怪的问题:有两个换行符,但只有一个标记 例如: code in line 1<br /> code in line 2<br /> 而不是: code in line 1 code in line 2 在标记中换行文本将强制其以书面形式显示:包括空格、制表符和新行。 <pre> preg_replace ("/[\n\r]+/", "",nl2br(file_get_contents("/cras

我对
pre
标记使用了
nl2br
函数,但我遇到了一个奇怪的问题:有两个换行符,但只有一个

标记

例如:

code in line 1<br />
code in line 2<br />
而不是:

code in line 1
code in line 2
标记中换行文本将强制其以书面形式显示:包括空格、制表符和新行。
<pre>
    preg_replace ("/[\n\r]+/", "",nl2br(file_get_contents("/crashbody.txt")))
</pre>

因此,回车符将创建一个新行,而

将创建第二个新行。

preg\u replace(“/\n+/”,“”,$pre)
或者更好的
preg\u replace(“/[\n\r]+/”,“”,$pre)
在预块中写入时,不需要应用nl2br()。

我也遇到了同样的问题。正确答案要简单得多。不要将nl2br与pre一起使用

nl2br为html文本添加了

,但pre标记已经保留了文本格式。 这就是它的意思<代码>=预格式化

<pre>
    file_get_contents("/crashbody.txt")
</pre>
是的,像这样的东西会起作用,直到它不起作用为止


preg_replace(“/[\n\r]+/”,“”,nl2br(文件获取内容(“/crashbody.txt”))
但这很愚蠢。您正在添加换行符并删除它们。为了保留空白和换行符,让
自己做吧


文件获取内容(“/crashbody.txt”)
或者更好:


文件获取内容(“/crashbody.txt”)

nl2br
将为每个
\r
和每个
\n
插入

,如果它们之间有任何字符。显示您的输入字符串。true,但我们如何在将其发送到浏览器之前删除新行?是否需要将其封装在
标记中?@Vahid-尝试
preg\u replace\u all(“//”,“”,$text)
感谢您的回复,但它只是删除了br不可见的换行符,在输出中只有一个br标记,但浏览器呈现两个换行符@瓦希德-在
 @瓦希德-你应该考虑更新你的问题,让它更清楚,向我们展示更多的输入/输出和代码,否则这只是一段时间,抱歉……问题被你的向导解决了,但是我替换了<代码> \r\n>代码>代码< > \\r\<代码>,看不见的行消失了!谢谢。
<pre>
    file_get_contents("/crashbody.txt")
</pre>
<div style = "white-space: pre; text-align:left;">
    file_get_contents("/crashbody.txt")
</div>