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.

    评论

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条