简体   繁体   中英

PHP preg_match result

This PHP function working just fine:

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

in

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:

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

But if I use the same php function in this case:

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:

xxx@yahoo.com
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 (xxxd@yahoo.com) [0] -  mta1010.mail.mud.yahoo.com

The preg_match function does not give any results. What am I doing wrong?

You are basically looking for a email addressing enclosed in < and > .

Your first input has it but the second input does not: It has no email id in < > hence you don't get a match.

Edit: (after your comment):

Looking at the 2 samples given by you the email id appears between the string failed: and the error code starting with 55 . You can make use of the preg_match for this as:

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

Ideone link

By default, preg_match doesn't support parsing of multiline string (String containing \\n), so you gotta pass in extra parameter to match the whole string, which is m (multiline).

try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM