【问题标题】:Strip/convert html artifacts from email html body in php/laravel从 php/laravel 中的电子邮件 html 正文中剥离/转换 html 工件
【发布时间】:2021-07-14 11:43:34
【问题描述】:

我在 laravel 中使用 IMAP 包来获取电子邮件的正文,但即使在进行了一些剥离之后,我仍然会得到很多 HTML 工件。

我现在正在使用这些功能:

        $styles_removed = preg_replace('/(<(script|style)\b[^>]*>).*?(<\/\2>)/is', "$1$3", $body);
        $fixed_body = strip_tags($styles_removed);
        $strip_body = utf8_decode($fixed_body);
        $formatted_body = preg_replace('/\s+|body {.*}/', ' ', $strip_body); 

完成此操作后,我正在转储 $formatted_body 并获取此示例:

I?m trying to start a thread &amp;nbsp; &amp;nbsp; Jon Doe &amp;nbsp;&amp;nbsp; Sales &amp;amp; Service &amp;nbsp;&amp;nbsp;

我是否缺少一个我应该在现有函数之上使用的函数,它将 &amp 和 nbsp 转换为其实际字符?

【问题讨论】:

    标签: php html laravel


    【解决方案1】:
    $formatted_body = 'I?m trying to start a thread &amp;nbsp; &amp;nbsp; Jon Doe &amp;nbsp;&amp;nbsp; Sales &amp;amp; Service &amp;nbsp;&amp;nbsp;';
    
    $content = preg_replace('/[^\S\r\n]+/',' ',trim(str_replace(['&nbsp;','&amp;'],[' ','&'],htmlspecialchars_decode($formatted_body ,ENT_QUOTES|ENT_COMPAT))));
    
    echo $content;
    

    哪个结果

    I?m trying to start a thread Jon Doe Sales & Service
    

    不知道?为什么在single quotes里面,应该是&amp;apos;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-26
      • 2012-04-28
      • 2017-07-24
      相关资源
      最近更新 更多