preg_split在项目前面的行

时间:2012-08-22 17:40:10

标签: php regex email preg-split regex-lookarounds

我正在创建自动电子邮件回复,但我不想在回复中显示原始电子邮件。电子邮件中有一行他们必须在上面做出回应。但电子邮件程序添加了一行,如“2012年8月21日晚上11:30,David写道:”在此行之前。

我正在使用此代码将响应拆分为两部分。它只是无法正常工作。

$parts = preg_split('/([\r|\n].+[\r|\n]>[\r|\n])?(> )?--- ABOVE THIS LINE ---/',$in->body);

它正在拆分的电子邮件正文

test from user back again

On Wed, Aug 22, 2012 at 9:55 AM, Support <support@example.com> wrote:

> --- ABOVE THIS LINE ---
>
>   Support Ticket

我想要做的就是在上面的线上分割---上面的线---位。换句话说,我想删除“On Wed,Aug 22 ...”这一行。我假设不是所有的电子邮件程序都放在这一行,如果他们这样做,他们会做不同的事情在此示例中,电子邮件程序实际上也添加了一个空行。

1 个答案:

答案 0 :(得分:0)

在这里你去!

/^.*wrote:(?s).* --- ABOVE THIS LINE ---/im

和解释:

# ^.*wrote:(?s).* --- ABOVE THIS LINE ---
# 
# Options: case insensitive; ^ and $ match at line breaks
# 
# Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
# Match any single character that is not a line break character «.*»
#    Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
# Match the characters “wrote:” literally «wrote:»
# Match the remainder of the regex with the options: dot matches newline (s) «(?s)»
# Match any single character «.*»
#    Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
# Match the characters “ --- ABOVE THIS LINE ---” literally « --- ABOVE THIS LINE ---»