【问题标题】:Checking whether the email content is a plain text or HTML - PHP检查电子邮件内容是纯文本还是 HTML - PHP
【发布时间】:2015-07-28 06:17:31
【问题描述】:

在 Code Igniter 框架上工作,我正在使用 Peeker 库检索电子邮件。有时我没有收到 HTML 的内容,只是电子邮件的 plain text。 对于纯文本,我想用<br> 替换\r\n,但不是HTML 内容。 我从SO 得到了下面的function 来检查内容是否是HTML:

function is_html($string)
{
  return preg_match("/<[^<]+>/",$string,$m) != 0;
}

以下是我收到的几条纯文本:

On 5/15/15, Mr.X  wrote:
> Mr. Y,
>
> Congratulations! Your book has been approved by our Editorial Board for
> Publishing. Please send me all the fonts that you have used to type
> your manuscript. Please send me the font names and the font files so that
> we can proceed.......

但该函数也为纯文本返回TRUE。 如何区分?

【问题讨论】:

  • 通过$message-&gt;get_header_array(); 你会得到内容类型 na ?我不习惯使用 Peeker,但在文档中找到了这个 ^^
  • 问题是我没有在数据库中存储内容类型。所以我必须从电子邮件内容中以某种方式区分
  • 我疯了...,你试过比较strlen($content)strlen(strip_tags($content))吗?
  • 或者你可以使用 "/]+>/"
  • @Bob0t 感谢您的建议,但我通过修改正则表达式解决了它,因为我知道几乎所有 html 内容中都会出现 div 或 table

标签: php html regex codeigniter


【解决方案1】:

由于我知道&lt;div&gt;&lt;table&gt; 标记将存在,因此我对正则表达式进行了以下更改,并且效果很好。

preg_match("/<tr [\s\S]*?<\/tr>|<table [\s\S]*?<\/table>|<div [\s\S]*?<\/div>",$string) != 0;

【讨论】:

    猜你喜欢
    • 2011-01-13
    • 2011-06-09
    • 2012-04-23
    • 1970-01-01
    • 2022-01-18
    • 2011-07-08
    • 2015-11-18
    • 2021-05-17
    相关资源
    最近更新 更多