从特定字符序列的最后一次出现开始删除所有字符 [英] Remove all characters starting from last occurrence of specific sequence of characters
问题描述
我正在解析一些电子邮件.我认为移动邮件,iPhone和iPod touch会附加一个签名作为单独的边界,从而使其易于删除.并非所有邮件客户端都这样做,仅使用'-'作为签名定界符.
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.
样本副本
hello, this is some email copy-- check this out
--
Tom Foolery
我考虑过拆分'-',删除最后一部分,我会得到,但是 explode()
和 split()
似乎都没有返回如果没有匹配项,让我知道它是否做了任何事情,都是非常有价值的.
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.
我无法获得 preg_replace()
跨越多行.我已经将所有行尾标准化为 \ n
.
I can not get preg_replace()
to go across more than one line. I have standardized all line endings to \n
.
以 hello结尾的最佳建议是什么,这是一些电子邮件副本-检查一下
,不这样做,在某些情况下没有签名,当然也有在某些情况下我无法涵盖所有情况.
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.
推荐答案
我认为,为了更加防弹,我将采用非正则表达式路线
I think in the interest of being more bulletproof, I will take the non regex route
echo substr($body, 0, strrpos($body, "\n--"));
这篇关于从特定字符序列的最后一次出现开始删除所有字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!