douxun4173 2012-07-06 17:24 采纳率: 0%
浏览 2270

如何用nl2br在html标签之间添加br?

我有一个 PHP 的 webmail 脚本,我今天收到了来自 gmail 的邮件,内容如下:

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

我怎样才能用 nl2br print它呢?

如果我用 echo nl2br($content);,结果是这样的:

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

所以我该如何修正这个问题?

  • 写回答

3条回答 默认 最新

  • dphphvs496524 2012-07-06 18:00
    关注

    Personally I think accepting html in a message is bad, and you should strip out any html, why?

    Because if you accept html tobe rendered. eg: not using htmlentities($content) from an unknown source then a malicious person could add javascript and XSS attack you, or a dodgy link and CSRF you or even just a 1px image and get your IP.

    But if you accept the risk you can just render the message as-is.

    <?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 is for content you expect to have no html in it.

    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条