The best way to chop off a signature from an email body

I am parsing a few letters. Mobile Mail, iPhone, and I assume that iPod touch adds the signature as a separate border, making it easier to delete. Not all email clients, and just use "-" as a signature separator.

I need to cut '-' from a string, but only in the latter case.

Copy example

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

I need to divide by "-" by deleting the last part, and I would get it, but explode () and split () do not return large values ​​to tell me if this did something, in case there is no match.

I cannot get preg_replace to go through multiple lines. I standardized all line endings for \ n

What is the best offer in the end, “hello, this is some kind of email - check it out”, without accepting, there will be cases when there is no signature, and, of course, there will be cases when I cannot cover all cases.

+3
source share
5 answers

I think it’s in the interest of being more bulletproof, I will take the route without regular expression

        echo substr($body, 0, strrpos($body, "\n--"));
+3
source

Actually the correct signature separator "-- \n" (note the space before the new line), so there should be a regexp separator '^-- $'. Although you can use it '^--\s*$', so it will work with OE, which makes it wrong.

+8
source

:

preg_replace('/--[\r\n]+.*/s', '', $body)

--, . , /.*--[\r\n]+.*/s .

+3

, - , , . , , , .

+3

, , :

$body = preg_replace('/\s*(.+)\s*[\r\n]--\s+.*/s', '$1', $body);

  • "( ) - ( / ) ()"
  • Trim the beginning / end of a space from the body (the remaining lines in front of the signature, spaces at the beginning of the body, etc.)
  • It will work only if there is some text in front of the signature (non-space) (otherwise it will not deprive the signature and will not be returned to it)
+2
source

Source: https://habr.com/ru/post/1706037/


All Articles