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?
Exactly what regular expressions are good for:
preg_match('/(?<=Check in date: )\S+/i', $string, $match);
echo $match[0];
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