Remove all characters starting from last occurrence of specific sequence of characters

I am parsing out some emails. Mobile Mail, iPhone and I assume iPod touch append a signature as a separate boundary, making it simple to remove. Not all mail clients do, and just use ‘–‘ as a signature delimiter.

I need to chop off the ‘–‘ from a string, but only the last occurrence of it.

Sample copy

 hello, this is some email copy-- check this out
 --
 Tom Foolery

I thought about splitting on ‘–‘, removing the last part, and I would have it, but explode() and split() neither seem to return great values for letting me know if it did anything, in the event there is not a match.

I can not get preg_replace() to go across more than one line. I have standardized all line endings to n.

What is the best suggestion to end up with hello, this is some email copy-- check this out, taking not, there will be cases where there is no signature, and there are of course going to be cases where I can not cover all the cases.

Answer

I think in the interest of being more bulletproof, I will take the non regex route

        echo substr($body, 0, strrpos($body, "n--"));

Leave a Reply

Your email address will not be published. Required fields are marked *