c_shelswell Posted August 27, 2007 Share Posted August 27, 2007 Hi I have a preg_match search that i'm now getting quite lost with. It should be pretty simple i'm checking bounced mails looking for the string: "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:" in the following: 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@email.com The mail server could not deliver mail to email@email.com. The account or domain may not exist, they may be blacklisted, or missing the proper dns entries. ------ This is a copy of the message, including all the headers. ------ Return-path: my code is: foreach ($delErrors as $key => $value) { $permError = "/".$value['string']."/"; if (preg_match($permError, $dtls['msg_body'], $found)) { // Finds a message with an error $errorType = $value['delivery_error_type']; } elseif (preg_match($permError, $dtls['subject'], $found)) { $errorType = $value['delivery_error_type']; } } i can't seem to get it to find the string at all. However if i shorten the search string to only "This is a permanent error." it works fine. Is there any reason why this might be happening? Many thanks Quote Link to comment Share on other sites More sharing options...
effigy Posted August 27, 2007 Share Posted August 27, 2007 Try passing $value['string'] through preg_quote. Quote Link to comment Share on other sites More sharing options...
c_shelswell Posted August 27, 2007 Author Share Posted August 27, 2007 Cheers, I'm still not having much joy though. I've updated the code to the below: foreach ($delErrors as $key => $value) { $permError = preg_quote("/".$value['string']."/"); $mBody = preg_quote($dtls['msg_body'], "/"); if (preg_match($permError, $mBody, $found)) { // Finds a message with an error $errorType = $value['delivery_error_type']; } elseif (preg_match($permError, $dtls['subject'], $found)) { $errorType = $value['delivery_error_type']; } } and it still doesn't seem to find anything. Any more ideas? Many thanks Quote Link to comment Share on other sites More sharing options...
effigy Posted August 27, 2007 Share Posted August 27, 2007 preg_quote should only be used to escape the content, not the delimiters. Therefore, preg_quote("/".$value['string']."/"); should be preg_quote($value['string'], '/');. Quote Link to comment Share on other sites More sharing options...
c_shelswell Posted August 27, 2007 Author Share Posted August 27, 2007 if i do that i then get the error message: "Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in" code is $permError = preg_quote('A message that you sent could not be delivered to one or more of its recipients. This is a permanent error.', '/'); $mBody = preg_quote($dtls['msg_body'], "/"); if (preg_match($permError, $mBody, $found)) { // Finds a message with an error $errorType = $value['delivery_error_type']; } Many thanks for your help! Quote Link to comment Share on other sites More sharing options...
effigy Posted August 27, 2007 Share Posted August 27, 2007 "Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in" Precisely. You need delimiters. Quote Link to comment Share on other sites More sharing options...
c_shelswell Posted August 27, 2007 Author Share Posted August 27, 2007 sorry to be a real pain i've been googling this for a bit now and i'm still really stuck. I've got it down to the fullstop at the end of the sentence "This message was created automatically by mail delivery software." if i add anything after the fullstop i can't get the preg_match to work. I've tried preg_quote and adding the backslashes in manually but i'm still not winning. Thanks for your help, it's very appreciated. Quote Link to comment Share on other sites More sharing options...
effigy Posted August 27, 2007 Share Posted August 27, 2007 Every preg expression requires delimiters. You had them, but you were escaping them, which was incorrect because they were being considered as part of the content, thus: not delimiters. After the content has been escaped (made regex-safe), the delimiters need to be reapplied. if (preg_match($permError should be if (preg_match("/$permError/". A working example: <pre> <?php $content = '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@email.com The mail server could not deliver mail to email@email.com. The account or domain may not exist, they may be blacklisted, or missing the proper dns entries. ------ This is a copy of the message, including all the headers. ------ Return-path:'; $look_for = '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:'; echo preg_match('/' . preg_quote($look_for, '/') . '/', $content) ? 'Matched' : 'Did Not Match'; ?> </pre> Quote Link to comment Share on other sites More sharing options...
c_shelswell Posted August 28, 2007 Author Share Posted August 28, 2007 Brilliant - thanks very much for your help got it working!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.