views:

9

answers:

1

Hi, I am trying to program a email piping php script that would take an incoming traffic update report via email, and extract the relevant information within it to store into a database.

The email usually starts with some introduction, with the important information displayed in the following format.

Highway : Some Highway 
Time : 08-Oct-2010 08:10 AM 
Condition : Smooth (or slow moving etc)

I tried with this code

preg_match_all('/(?P<\name>\w+) : (?P<\data>\w+)/i', $subject, $result);

Note the < / are really just < but somehow they are not being displayed here.

And the matches are only:

Highway : Some
Datetime : 08
Condition : Smooth

Can anybody tell me what's missing in my second regex expression? Why doesn't it include the entire string of words after the ":"?

+1  A: 

You are capturing \w+. That only matches word characters, this does not include spaces or parenthesis.

Try

preg_match_all('/(?P<name>\w+)\s*:\s*(?P<data>.*)/i', $subject, $result);

try using .*? This will match everything up to the new line character

Galen
Thank you so much Galen! May I know what exactly does * do?
blacklotus
@blacklotus: The Kleene star (`*`) in a regular expression signifies zero or more repetitions of something. In this answer, `\s*` means zero or more whitespace characters. The zero part is important, since something like `ab*a` would match `aba` and `abbbbba` but *also* `aa`.
eldarerathis
@eldarerathis Thanks!
blacklotus