2010-12-01 103 views
1

这个PHP函数工作得很好:PHP的preg_match导致

if (preg_match ("/<(\[email protected]\S+\w)>.*\n?.*55[0-9]/i",$body,$match)) { 
echo "found!"; 
} 

This message was created automatically by mail delivery software. 

A message that you sent could not be delivered to one or more of its 
recipients. This is a permanent error. The following address(es) failed: 

[email protected] 
SMTP error from remote mail server after RCPT TO:<[email protected]>: 
host mx3.hotmail.com [65.54.188.72]: 550 Requested action not taken: 
mailbox unavailable 

但是,如果使用相同的PHP函数在这种情况下:

A message that you sent could not be delivered to one or more of its 
recipients. This is a permanent error. The following address(es) failed: 

[email protected] 
SMTP error from remote mail server after end of data: 
host d.mx.mail.yahoo.com [209.191.88.254]: 554 delivery error: 
dd This user doesn't have a yahoo.com account ([email protected]) [0] - mta1010.mail.mud.yahoo.com 

preg_match功能没有给出任何结果。我究竟做错了什么?

回答

4

你基本上是寻找一个电子邮件给封闭在<>

你的第一个输入有它,但第二个输入没有:它在< >没有电子邮件ID,因此你没有得到一个匹配。

编辑:(您的评论后):

看着你所提供的电子邮件地址的2个样品出现的字符串failed:并开始55错误代码之间。您可以使用的preg_match的这个为:

if(preg_match('/failed:.*?(\b[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}\b).*55[\d]/is',$str,$m)) { 
     echo "Bounced email ".$m[1]."\n"; 
} 

Ideone link

0

默认情况下,preg_match不支持多行字符串的解析(包含\ n的字符串),所以您必须传入额外的参数以匹配整个字符串,即m(多行)。

尝试

if (preg_match ("/<(\[email protected]\S+\w)>.*\n?.*55[0-9]/im",$body,$match)) { 
echo "found!"; 
} 
+0

我都试过了,但都没有成功。 – Sergio 2010-12-01 16:29:49

+0

好的,你能明确告诉我们你想要匹配什么吗? – 2010-12-01 16:34:16