2013-02-26 52 views
0

我正尝试从一个转发的电子邮件,您可以通过电子邮件和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]> 

它看起来像电子邮件正在影响正则表达式。但是,怎么样,为什么它不影响它呢?我怎样才能解决这个问题?

+1

工作对我来说:http://regex101.com/r/eQ6iS8在第二次看,我看到了一些问题。如果您将最后一个电子邮件地址更改为“发件人”,它会以您目前看到的方式失败正则表达式太贪婪。我会看看我能否找到解决方案。 – mkaatman 2013-02-26 18:51:37

回答

3

问题是,\D*匹配太多,即它也匹配换行符。我会在这里更受限制。为什么你使用\D(不是数字)呢?

用例如[^@]*它正在

cc: [^@]*\S([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})\S 

看到它here on Regexr

这样,您确定第一部分不匹配超出电子邮件地址。

这也是\D的原因,它是第一个工作,“从”的情况下。 “日期”行中有数字,因此它不匹配该行。

1

尝试这样

$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]'; 

$pattern = '#(?:from|Cc):\s+[^<>]+<([^@][email protected][^>\s]+)>#is'; 
preg_match_all($pattern, $body, $arr_matches); 
echo '<pre>' . htmlspecialchars(print_r($arr_matches, 1)) . '</pre>'; 

输出

Array 
(
    [0] => Array 
     (
      [0] => From: Sarah Johnson <[email protected]> 
      [1] => Cc: Ralph Johnson <[email protected]> 
     ) 

    [1] => Array 
     (
      [0] => [email protected] 
      [1] => [email protected] 
     ) 

) 

$arr_matches[1][0] - "From" email 
$arr_matches[1][1] - "Cc" email 
+0

@MikeM更正。谢谢! – Winston 2013-02-26 19:42:04