NL2BR 将 BR 放在 HTML 标签之间


nl2br puts br between html tags

我有一个PHP网络邮件脚本,我今天从gmail收到这样的消息:

$content='
<p>This is just example for MessageContent</
p>Test...<span>Test</span>Test..<span
>Test</span>';

如何用nl2br打印?

如果我使用 echo nl2br($content); ,我会得到这个结果:

<br />
<p>This is just example for MessageContent</<br />
p>Test...<span>Test</span>Test..<span<br />
>Test</span>

那么我该如何解决这个问题呢?

我个人认为在消息中接受html是不好的,你应该删除任何html,为什么?

因为如果你接受html渲染。 例如:不使用来自未知来源的htmlentities($content),那么恶意的人可能会添加javascript和XSS攻击你,或者一个狡猾的链接和CSRF你,甚至只是一个1px的图像并得到你的IP。

但是,如果您接受风险,则可以按原样呈现消息。

<?php 
$content='
<p>This is just example for MessageContent</
p>Test...<span>Test</span>Test..<span
>Test</span>';
//Or at least sanitise abit - Remove all tags except p's an a's
$content = strip_tags($content,'<p><a>');
?>

NL2BR适用于您希望其中没有HTML的内容。

您可以将

$content更改为

$content='
<p>This is just example for MessageContent</p>
Test...<span>Test</span>Test..
<span>Test</span>';

或者你可以使用

echo $content;