我nl2br
对标签使用了函数pre
,但我遇到了一个奇怪的问题:有2个换行符,但只有一个<br />
标签。
例如:
code in line 1<br />
code in line 2<br />
显示为:
code in line 1
code in line 2
代替:
code in line 1
code in line 2
我nl2br
对标签使用了函数pre
,但我遇到了一个奇怪的问题:有2个换行符,但只有一个<br />
标签。
例如:
code in line 1<br />
code in line 2<br />
显示为:
code in line 1
code in line 2
代替:
code in line 1
code in line 2
将文本包装在<pre>
标签中将强制它以书面形式显示:包括空格、制表符和换行符。因此,回车将创建一个新行并且<br />
将创建第二个新行。
I had the same problem. The correct answer is much simpler. Don't use nl2br with pre.
nl2br adds <br />
to text for html, but the pre tag already preserves the text format.
That's what it means. <pre>
= preformatted.
Yes, something like this will work, until it doesn't.
<pre>
preg_replace ("/[\n\r]+/", "",nl2br(file_get_contents("/crashbody.txt")))
</pre>
But that's silly. You're adding line breaks and removing them. To preserve your whitespace and your line breaks, let <pre>
do it's job.
<pre>
file_get_contents("/crashbody.txt")
</pre>
Or better still:
<div style = "white-space: pre; text-align:left;">
file_get_contents("/crashbody.txt")
</div>
当您在 pre 块中编写它时,您不需要应用 nl2br() 。
preg_replace ("/\n+/", "", $pre)
甚至更好preg_replace ("/[\n\r]+/", "", $pre)