Help with preg_replace

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm new to this regular expression stuff. I'd like to use preg_replace to
eliminate a known multi-line signature from the body of an E-mail. Say the
body text is in $body, and the sig is this

Sig line1
Sig line2
Sig line3

If I could just get rid of that, it would be pretty good. But I also get this
kind of junk a lot, since messages are being quoted:

Quoted text here. Click to load it


so I thought I'd be smart and tried:

$body = preg_replace("/---.*?Sig line1.*?Sig line2.*?Sig line3/","",$body);

but this erased the entire message somehow. So I thought I'd go back to
basics and tried:

$body = preg_replace("---","",$body);
$body = preg_replace("Sig line1","",$body);
$body = preg_replace("Sig line2","",$body);
$body = preg_replace("Sig line3","",$body);

but this erased everything too.

I'm kinda stumped. Why are these erasing the entire message? And what's the
actual smart way to erase this signature when it can have any amount of white
space and >'s between lines?

Thanks for any help.

Re: Help with preg_replace

Regarding the first regex you need to set the multiline flag
to 's', although to be on the safe side you can add 'i' in order to
ignore case.
Therefore you're php code should end up like so:

$body = preg_replace("/---.*?Sig line1.*?Sig line2.*?Sig

More clearly:
(  "/<regex here>/si", "", $body  );

To remove quotes you should put something like:
$body = preg_replace('/>.*/', '', $body);
without '//si' !

In summary 'si' comes in very handy at times.

Re: Help with preg_replace

On 6/20/2005 1:08:49 AM, "merlinfly" wrote:
Quoted text here. Click to load it

Any idea why my syntax blew away the whole text though? Sounds like I should
be getting fewer matches if it wouldn't go across lines.

Re: Help with preg_replace

$body = preg_replace("/---.*?Sig line1.*?Sig line2.*?Sig
t didn't blow away the lines; when preg_replace() does not find a match
it returns an emtpy string, ''.   That's just how preg_replace() works.
without the //si you would be telling it to search single lines which
contain  the  dashes and Sig line x separated by anything apart from a
new line.

You could take it as a signal to perfect your regular expression from
that point on.

Site Timeline