2

I'm trying to write a script that parses an email and looks for certain strings after certain strings.

For instance, let's say i have the following email body.

$body = "Dear John Doe, your information is below
Check in date: 07/23/2012
Check out date: 07/26/2012

Alternate Check in date: 07/27/2012
Alternate Check out date: 07/29/2012";

I want to grab the 'word' directly after the first instance of "Check in date:", which would be "07/23/2012". The 'word' being the next set of characters with spaces around it. I included the 'alternate' parts because some emails will have a text version and an html version.

I think i need to use a combo of strpos(), substr(), and explode() but i'm not sure how. Any suggestions?

1
  • Show what you've attempted and we'll try to help fix it. But right now this is a thinly veiled "do my job for me" non-question. Commented Jul 10, 2012 at 15:02

2 Answers 2

7

Exactly what regular expressions are good for:

preg_match('/(?<=Check in date: )\S+/i', $string, $match);
echo $match[0];

http://www.regular-expressions.info

Sign up to request clarification or add additional context in comments.

Comments

3

This is a good use-case for regexes:

if (preg_match('#^Check in date: ([^\s]+)#m', $body, $match)) {
    $result = $match[1];
}

Try it here: http://codepad.viper-7.com/W4UW4P

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.