By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,650 Members | 1,785 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,650 IT Pros & Developers. It's quick & easy.

Help with preg_replace

P: n/a
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:
---
Sig line1
Sig line2
Sig line3


or
---
Sig line1
Sig line2
Sig line3


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.
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
line3/si","",$body);

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.

Jul 17 '05 #2

P: n/a
On 6/20/2005 1:08:49 AM, "merlinfly" wrote:
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
line3/si","",$body);

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.


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.
Jul 17 '05 #3

P: n/a
Regarding:
$body = preg_replace("/---.*?Sig line1.*?Sig line2.*?Sig
line3/","",$body);
I
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.

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.