我正尝试从一个转发的电子邮件,您可以通过电子邮件和CC时,身体看起来是这样的:正则表达式不工作
$body = '-------
Begin forwarded message:
From: Sarah Johnson <[email protected]>
Subject: email subject
Date: February 22, 2013 3:48:12 AM
To: Email Recipient <[email protected]>
Cc: Ralph Johnson <[email protected]>
Hi,
hello, thank you and goodbye!
[email protected]'
现在,当我做到以下几点:
$body = strtolower($body);
$pattern = '#from: \D*\S([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})\S#';
if (preg_match($pattern, $body, $arr_matches)) {
echo htmlentities($arr_matches[0]);
die();
}
我得到正确:
from: sarah johnson <[email protected]>
现在,为什么CC不工作?我做的非常类似的东西,只能从到抄送变化:
$body = strtolower($body);
$pattern = '#cc: \D*\S([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})\S#';
if (preg_match($pattern, $body, $arr_matches)) {
echo htmlentities($arr_matches[0]);
die();
}
,我也得到:
cc: ralph johnson <[email protected]> hi, hello, thank you and goodbye! [email protected]
如果我从原来的身体页脚删除电子邮件(除去[email protected]),那么我正确得到:
cc: ralph johnson <[email protected]>
它看起来像电子邮件正在影响正则表达式。但是,怎么样,为什么它不影响它呢?我怎样才能解决这个问题?
工作对我来说:http://regex101.com/r/eQ6iS8在第二次看,我看到了一些问题。如果您将最后一个电子邮件地址更改为“发件人”,它会以您目前看到的方式失败正则表达式太贪婪。我会看看我能否找到解决方案。 – mkaatman 2013-02-26 18:51:37