preg_replace vs preg_match

see this: http://www.damnsemicolon.com/php/php-parse-email-body-email-piping

$body contains the complete email body including quoted area if this is a reply, this loop removes quoted area to get new reply as $message. But as suggested there, loop is slow and better to use preg_replace instead. so how can I do?

replace patterns with what? should I remove foreach loop too? I created below without foreach loop but seems wrong? please advice.

$patterns = array(
"_________________________________________________________________",
"/^-*(.*)Original Message(.*)-*/i",
"/^On(.*)wrote:(.*)/i",
"/^On(.*)$fromName(.*)/i",
"/^On(.*)$toName(.*)/i",
"/^(.*)$toEmail(.*)wrote:(.*)/i",
"/^(.*)$fromEmail(.*)wrote:(.*)/i",
"/^>(.*)/i",
"/^---(.*)On(.*)wrote:(.*)/i");

$message = preg_replace($patterns, '', $body);

if you are trying to replace a string, the pre_replace can serve as its own ‘foreach’ loop. so you don’t have to worry about that part, especially if the replacement string is always the same. I am not certain if using the builtin loop will do anything to speed up the function or not.

using if you were looking for specific strings, a str_replace, str_ireplace are much faster!

for example if you re ALWAYS looking for a string containing 30 “_” characters, you could do that with a str_replace.

Consider checking your reggae logic as well. For example:
“/^-(.)Original Message(.)-/i”
will select a string that begins with -(0- infinite times), good so far.
followed any number of any non newline characters ( which means essentially the only stop would be a tab or a break or something). This selection will be kind of broad, and thus it will never even get to look for :“Original Message”, let along the other string of characters of undefined length that follows. :confused:

Thanks for trying to help, would you please re-write that code block on link using preg_replace that I understand better exactly? Note that not all patterns begin with O. so I guess we will still have 7 patterns? can they be in an array and replace all of them in just one preg_replace? replace to what to remove quoted part? with blank “”? if you provide a re-written code block I would be grateful.
Look forward hearing from you.

Yes. preg_replace can accept arrays for both what is to be searched for and what to replace it with so that all the replaces can be done with a single call.

Hi, thanks for trying to help, but that link I gave uses pre_match within a loop with break command that when it meets the pattern criteria, just breaks the loop to not appened the rest of email line to the message and this way, quoted area will be removed. but with preg_replace how can I have IF statement with break command to kick it out from the loop to not appened the rest of emal to the message? preg_match does not return anything, it just gives $matches as array as 3rd param, but with preg_replace it returns something, so how to re-write the code on that link with preg_replace to do the same behavior?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.