Jump to content

[SOLVED] Preg match not picking this string up.


c_shelswell

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.