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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Find RGB colors from a screen. 2 39
Get data from two MySQL tables 6 65
Google Recaptcha 13 51
Form not executing correctly 1 26
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

737 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