I need a regular expression to parse a body of text. Basically assume this that we have text files and each of which contains random text but within the text there would be lines in the following formats - basically they are a format for denoting flight legs.
eg:
13FEB2009 BDR7402 1000 UUBB 1020 UUWW FLT
This line of text is always on one line
The first word is a date in the format DDMMMYYYY
Second word could be of any length and hold alphanumeric characters
third word is the time in format HHMM - its always numeric
fourth word is a location code - its almost always just alphabets but could also be alphanumeric
fifth word is the arrival time in format HHMM - its always numeric
sixth word is a location code - its almost always just alphabets but could also be alphanumeric
Any words which follow on the same line are just definitions
A text file may contain among lots of random text information one or more such lines of text.
I need a way to be able to extract all this information i.e just these lines within a text file and store them with thei开发者_JS百科r integral parts separated as mentioned in an associative array so I have something like this:
array('0'=>array('date'=>'', 'time-dept'=>'', 'flightcode'=>'',....))
I'm assuming regular expressions would be in order here. I'm using php for this - would appreciate the help guys :)
I'm not in a position to test this, and my PHP is rusty, but I think this should do what you need:
foreach ($lines as &$line) {
$matchcount = preg_match('/([0-9]{2}[A-Z]{3}[0-9]{4}) ([A-Z0-9]+) ([0-9]{4}) ([A-Z0-9]+) ([0-9]{4}) ([A-Z0-9]+)/', $line, $matches);
if ($matchcount > 0) {
$flight_data = array('date'=>$matches[1], 'flightcode'=>$matches[2], 'time-dept'=>$matches[3], 'loc1'=>$matches[4], 'time-arrv'=>$matches[5], 'loc2'=>$matches[6]);
// then do something with flight_data here
}
}
Edit: Fixed the missing $'s Brendan caught. Thanks!
May be functions fgetcsv and str_getcsv helps you.
$str_data = "blablabal";
$ar_data = str_getcsv($str_data, ' ', '');
And then you use this $ar_data array, as indexed array.
精彩评论