2010-12-01 139 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