I am trying to program a email piping php script that would take an incoming traffic update report via email, and extract the relevant informati开发者_Go百科on 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 ":"?
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
精彩评论