Help with preg_replace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Charles

    Help with preg_replace

    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:
    [color=blue]
    > ---
    > Sig line1
    > Sig line2
    > Sig line3[/color]

    or
    [color=blue][color=green]
    >> ---
    >> Sig line1
    >> Sig line2
    >> Sig line3[/color][/color]

    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("S ig line1","",$body );
    $body = preg_replace("S ig line2","",$body );
    $body = preg_replace("S ig 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.
  • merlinfly

    #2
    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
    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.

    Comment

    • Charles

      #3
      Re: Help with preg_replace

      On 6/20/2005 1:08:49 AM, "merlinfly" wrote:[color=blue]
      >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.
      >
      >[/color]

      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.

      Comment

      • merlinfly

        #4
        Re: Help with preg_replace

        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.

        Comment

        Working...