parsing previous body

Posted on 2013-02-04
Last Modified: 2013-02-04
words words words<br>words.

On Mon, Jan 2, 2012 at 8:48 PM,

Open in new window

words words words<br>words.

On Wed, Feb 12, 2012 at 10:48 PM,

Open in new window

I am parsing gmail bodys

These are examples of the gmail body where the body ends and the previous body is displayed

I do not want to see the previous body, so I would like to strip everything after and including

[new line][new line]
On[space][day of week][comma][space][month][space][day of month][comma][space][year][space]at[time of day][space][am or pm]
Question by:rgb192
  • 2
  • 2
LVL 35

Expert Comment

by:Terry Woods
ID: 38853182
A regex for the pattern you're describing is:
\n\nOn \w{3}, \w{3} \d\d?, \d{4} at \d\d?:\d\d [AP]M

Open in new window

You missed a space before the time of day, so I've added it. You may like to add the comma to the end too for completeness.

In some cases I find there are also line feed characters in between newlines. They can be accounted for (without being required) like this:
\r?\n\r?\nOn \w{3}, \w{3} \d\d?, \d{4} at \d\d?:\d\d [AP]M

Open in new window


Author Comment

ID: 38853197
words words words<br>words.

On Mon, Jan 2, 2012 at 8:48 PM,

Open in new window

how can I write $smaller_body=replace(pattern,$body)
LVL 35

Accepted Solution

Terry Woods earned 500 total points
ID: 38853279
Try this:

$smaller_body=preg_replace("#^(.*?)\r?\n\r?\nOn \w{3}, \w{3} \d\d?, \d{4} at \d\d?:\d\d [AP]M.*#s","$1",$body)

Open in new window

(update: fixed bug)

Author Closing Comment

ID: 38853501
I have a related question for html gmail body

works thanks

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mimic google as my ip 11 53
change password links 7 72
PHP_POST() error message 9 40
Need some help with regex 3 25
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now