在nl2br中使用表标签时如何避免不必要的br标签?



我有一个变量文本,有时包含纯文本,有时包含代码,有时包含表格。

我想最小化数据库中的 HTML 标签,所以我没有在tabletext中使用pre标签,只在代码中使用(其他两种情况不需要保留空间(。

现在我遇到了一个问题,我tables作为我的数据,这些数据正在通过nl2br,导致我的源代码中出现以下输出。

<table><br />
<thead><br />
<tr><br />
<th>Operator</th><br />
<th>Left</th><br />
<th>Right</th><br />
<th>Remark</th><br />
</tr><br />
</thead><br />
<tbody><br />
<tr><br />
<td>/</td><br />
<td>10</td><br />
<td>5 or 5 / 2 / 1</td><br />
<td>Left operand is unambiguous, right is not</td><br />
</tr><br />
<tr><br />
<td>/</td><br />
<td>5 or 10 / 5</td><br />
<td>2 or 2 / 1<td><br />
<td>Both left and right are ambiguous</td><br />
</tr><br />
<tr><br />
<td>/</td><br />
<td>2 or 10 / 5 / 2</td><br />
<td>1</td><br />
<td>Right operand is unambiguous and left is not</td><br />
</tr><br />
</tbody><br />
</table><br />

但是在chrome开发人员的工具中,它显示的内容与此不同。

</p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> 
<br><br><br><br><br><br><br><br><br><br>
<table class="rembr">
<thead>
<tr>
<th>Operator</th>
<th>Left</th>
<th>Right</th>
<th>Remark</th>
</tr>
</thead>
<tbody>
<tr>
<td>/</td>
<td>10</td>
<td>5 or 5 / 2 / 1</td>
<td>Left operand is unambiguous, right is not</td>
</tr>
<tr>
<td>/</td>
<td>5 or 10 / 5</td>
<td>2 or 2 / 1</td><td>
</td><td>Both left and right are ambiguous</td>
</tr>
<tr>
<td>/</td>
<td>2 or 10 / 5 / 2</td>
<td>1</td>
<td>Right operand is unambiguous and left is not</td>
</tr>
</tbody>
</table><br>

我不明白为什么同一页面的输出不同。

好吧,现在,我想知道如何在nl2br中使用表标签,而无需在每个table标签后都有br

经过几天的谷歌搜索,我为此找到了一个完美的解决方案。pre-wrap.

它将保留空格和新行,但在溢出文本时换行单词。

所以

div{
white-space: pre-wrap;
}

最新更新