我正在开发一个 PHP 票证系统,我可以在其中通过管道传输电子邮件、获取其 HTML 并插入到数据库中。
我已将此行添加到我的外发电子邮件中:
## If you reply, text above this line is added to the request ##
在 Upwork 电子邮件中看到这种类型的东西,很容易在该唯一字符串之前只抓取 email/html,使用:
//now, get only the stuff before our "dividing" line starts
$html = strstr($html, '## If', true) ?: $html;
无论如何,我注意到 Gmail 会自动将以下内容添加到所有电子邮件回复中:
On Fri, Jun 7, 2019 at 2:40 PM Carson Wentz<[email protected]> wrote:
因此,在我执行第一步后只保留“## If you reply...”之前的内容后,我现在想搜索剩余的 text/html 以查看它是否有以“On”开头并以“wrote”结尾的字符串:”。如果是这样,只在此之前抓取东西(类似于步骤 1)。
我很难找到任何清楚地解释如何在较长的字符串中搜索以特定内容开头并以特定内容结尾的较短字符串的内容,无论中间是什么。我想它必须使用正则表达式?
然而,当我写这篇文章时,我才意识到很可能在某个时候有人可能会以“开”开始他们的回复,在这种情况下,一切都会被删除。啊。
如果有人有任何想法是否可以处理,请告诉我。我想得更多,我可能只需要在票务系统内的所有回复中都显示包含 Gmail 的行,因为我认为没有绝对的方法可以获得那个确切的字符串,因为它包括日期/时间和名称显然总是不同的信息。
谢谢你的时间。
您可以使用preg_replace
和以下模式:
/^(?:On .+?> wrote:)?((\R|.)+?)## If you reply, text above this line is added to the request ##/
这可以选择匹配一个文字On
,然后是> wrote:\n
从正文字符串开始的任何字符,然后捕获所有内容,直到终止消息,包括带有 的换行符\R
。
当然,你可以走得更远,使头型更加严格,但它似乎很不太可能有人会写On [any characters...]> wrote:\n
上准确的第一线,这是一个假阳性,并会造成信息丢失。走严格的路线可能会遇到极端情况,即不寻常的电子邮件地址会导致误报并被错误地视为正文的一部分。
下面的示例表明,即使此标题出现在第一行之后的任何位置,它也会被视为正文的一部分。
^\s*On
如果On...
开始前可能有空格,请使用。
<?php
$withGmailHeader = "On Fri, Jun 7, 2019 at 2:40 PM Carson Wentz<[email protected]> wrote:
Here's the text content of the email. We'd like to extract it.
On Fri, Jun 6, 2019 at 2:53 AM Bob Smith<[email protected]> wrote:
'hello'
## If you reply, text above this line is added to the request ##";
$withoutGmailHeader = "On Fri, Jun 7, 2019 at 2:40 PM Carson Wentz<[email protected]> wrote:
Here's the text content of the email. We'd like to extract it.
On Fri, Jun 6, 2019 at 2:53 AM Bob Smith<[email protected]> wrote:
'hello'
## If you reply, text above this line is added to the request ##";
$pattern = "/^(?:On .+?> wrote:)?((\R|.)+?)## If you reply, text above this line is added to the request ##/";
preg_match($pattern, $withGmailHeader, $match);
echo "\n=> With Gmail header:\n";
var_export($match[1]);
echo "\n\n=> Without Gmail header: (note the extra space after >)\n";
preg_match($pattern, $withoutGmailHeader, $match);
var_export($match[1]);
输出:
=> With Gmail header:
'
Here\'s the text content of the email. We\'d like to extract it.
On Fri, Jun 6, 2019 at 2:53 AM Bob Smith<[email protected]> wrote:
\'hello\'
'
=> Without Gmail header (note the extra space after >):
'On Fri, Jun 7, 2019 at 2:40 PM Carson Wentz<[email protected]> wrote:
Here\'s the text content of the email. We\'d like to extract it.
On Fri, Jun 6, 2019 at 2:53 AM Bob Smith<[email protected]> wrote:
\'hello\'
'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句