I have a text file that contains the following lines, which I have read into an array called $pirep.
06:36 Block to Block Duratio
n: 07:37
06:36 Final Fuel: 1044 Lbls
I have written code to find "Block to Block Duration" and record the time that follows into a database. The problem is that the code cannot find the string because the text file is broken down into 1KB chunks, and that happened to fall right in the middle of my search string.
I understand how to use str_replace(PHP_EOL, '') to join the two strings, and that would be great if the data were written at the same place every time, but it isn't.
Is there a way to recognize the time pattern that precedes every line and combine lines until it finds the next occurrence of that pattern, so that the end result looks like:
06:36 Block to Block Duration: 07:37
06:36 Final Fuel: 1044 Lbls
This is only my second question, and I'm really hoping not to get on anyone's nerves with this. Thanks!
EDIT
Here is the code I am using to parse the text file;
//Read temp file to array
$pirep=file("temp.txt");
$num=count($pirep);
//Assign known variables
$pnum=explode(":",$pirep[2]);
$dca=$pnum[1];
$fltnum=explode(":",$pirep[1]);
$flt=$fltnum[1];
$date=$pirep[0];
//$dep=$pirep[5];
//$dest=$pirep[6];
//Determine if this is a mainenance division flight, set variable accordingly
for($i=0;$i<$num;$i++) {
$line=$pirep[$i];
if(strpos($line,"MAINT")!==FALSE) {
$maint="yes"; }
}
//Cycle through pirep array and find departure and destination airports
for($i=0;$i<$num;$i++) {
$line=$pirep[$i];
if(strpos($line,"Departing Airport")!==FALSE) {
$dep=$pirep[$i]; }
}
for($i=0;$i<$num;$i++) {
$line=$pirep[$i];
if(strpos($line,"Destination Airport")!==FALSE) {
$dest=$pirep[$i]; }
}
//Cycle through pirep and find Block to Block Duration
for($i=0;$i<$num;$i++) {
$line=$pirep[$i];
if(strpos($line,"Block to Block Duration")!== FALSE) {
$duration=ltrim(strstr($line,"Duration"),"Duration:");
$flttime=explode(":",$duration);
$minutes=($flttime[0]*60)+$flttime[1]; } }
//Cycle through pirep and find comments if any exist
for($i=0;$i<$num;$i++) {
$line=$pirep[$i];
if(strpos($line,"comments")!== FALSE) {
$raw_comments=ltrim(strstr($line,"comments "),"comments");
$comments=addslashes($raw_comments); } }
//Connect to DCA and find pilot
include('connect_db.php');
$stmt="SELECT * FROM newpilots WHERE dca=$dca LIMIT 1";
$result=mysql_query($stmt);
if(!$result) {echo"<BR> Query error<BR>"; }
if(mysql_num_rows($result)==0) {echo "<BR>Pilot $dca Not Found<br>"; }
$count=mysql_num_rows($result);
$lastname=mysql_result($result,0,'lastname');
$firstname=mysql_result($result,0,'firstname');
$current_minutes=mysql_result($result,0,'minutes');
$email=mysql_result($result,0,'email');
$name=$firstname." ".$lastname;
//Check for empty maint and comment variables
if(!isset($maint)) {$maint="no";}
if(!isset($comments)) {$comments="None";}
$timestamp=time();
$stmt="INSERT INTO pireps VALUES('','$dca','$name','$flt','$maint','$minutes','$comments','$timestamp')";
$result=mysql_query($stmt);
if(!$result) {echo "Query Error";}
$updated_minutes = $current_minutes + $minutes;
$stmt="UPDATE newpilots SET minutes='$updated_minutes', lastfile='$timestamp', flag='YES' WHERE dca=$dca";
$result=mysql_query($stmt);
if(!$result) {echo "Query Error Updating Roster"; }
$to = 'email@hidden.com, ';
$headers = 'Cc: '.$email."
";
$subject = "DC-3 Airways FSAcars Pirep Verification";
if($maint=="yes") { $headers = 'Cc: also.hidden.com' . "
"; }
$headers .= 'From: admin@dcascreenshots.net'."
";
$headers .= 'Reply-To: admin@dcascreenshots.net'."
";
$body = "This is an automatic email generated by the DC-3 Airways FSAcars system.
The following information has been posted;
Date Submitted: $date
Pilot $dca
Flight Number: $flt
$dep
$dest
Duration: $duration
Pilot's Comments:
$comments
Please log in to the APS system to verify your hours. If there is a discrepancy, please contact a staff member.";
if (!mail($to, $subject, $body, $headers)) {
echo "Email Not Sent.<br>"; }
and here is a sample text file;
[2015/05/30 22:58:00]
Flight IATA:PAJN-PADU
Pilot Number:0309
Company ICAO:DCA
Aircraft Type:DC3
Departing Airport: PAJN
Destination Airport: PADU
Online: No
Route:RUKIE V317 CSPER T278 HAPIT V440 CENTA T269 YAK V440 MDO T237 HOM V321 AKN T271 BINAL T227 MORDI G8 ZEBUV
Flight Level:10000
22:58 Zero fuel Weight: 21531 Lbs, Fuel Weight: 4664 Lbs
22:59 Parking Brakes off
22:59 Take-off
22:59 Take off Weight: 26187 Lbs
22:59 Wind: 261º @ 010 Knots Heading: 281º
22:59 POS N58º 21´ 17´´ W134º 34´ 32´´
22:59 Gear Up: 119 Knots
23:10 TOC
23:10 Fuel Weight: 4545 Lb
23:14 Wind:313º@001 Knots Heading: 272º Ground Speed: 166 Knots Altitude 9996 ft
23:29 Wind:322º@004 Knots Heading: 271º Ground Speed: 165 Knots Altitude 10059 ft
23:44 Wind:313º@007 Knots Heading: 320º Ground Speed: 160 Knots Altitude 10042 ft
23:59 Wind:297º@007 Knots Heading: 318º Ground Speed: 160 Knots Alt
itude 10049 ft
00:14 Wind:273º@008 Knots Heading: 317º Ground Speed: 161 Knots Altitude 10052 ft
00:28 Simulation Rate 2x
00:29 Wind:279º@009 Knots Heading: 270º Ground Speed: 158 Knots Altitude 10055 ft
00:44 Wind:289º@012 Knots Heading: 270º Ground Speed: 156 Knots Altitude 10072 ft
00:59 Wind:292º@012 Knots Heading: 269º Ground Speed: 157 Knots Altitude 10072 ft
01:14 Wind:288º@011 Knots Heading: 268º Ground Speed: 158 Knots Altitude 10075 ft
01:29 Wind:273º@009 Knots Heading: 266º Ground Speed: 159 Knots Altitude 10078 ft
01:44 Wind:261º@007 Knots Heading: 296º Ground Speed: 163 Knots Altitude 10062 ft
01:59 Wind:260º@005 Knots Heading: 295º Ground Speed: 164 Knots Altitude 10032 ft
02:14 Wind:238º@003 Knots Heading: 294º Ground Speed: 167 Knots Altitude 10026 ft
02:29 Wind:345º@004 Knots Heading: 295º Ground Speed: 166 Knots Altitude 10082 ft
02:44 Wind:356º@0
04 Knots Heading: 238º Ground Speed: 171 Knots Altitude 10009 ft
02:59 Wind:040º@003 Knots Heading: 236º Ground Speed: 173 Knots Altitude 10022 ft
03:14 Wind:070º@004 Knots Heading: 236º Ground Speed: 173 Knots Altitude 10052 ft
03:29 Wind:111º@008 Knots Heading: 233º Ground Speed: 174 Knots Altitude 10045 ft
03:44 Wind:137º@015 Knots Heading: 221º Ground Speed: 170 Knots Altitude 10036 ft
03:59 Wind:174º@015 Knots Heading: 221º Ground Speed: 163 Knots Altitude 10068 ft
04:14 Wind:182º@016 Knots Heading: 220º Ground Speed: 158 Knots Altitude 9940 ft
04:29 Wind:172º@022 Knots Heading: 218º Ground Speed: 156 Knots Altitude 9885 ft
04:44 Wind:164º@030 Knots Heading: 214º Ground Speed: 153 Knots Altitude 9921 ft
04:59 Wind:168º@031 Knots Heading: 214º Ground Speed: 151 Knots Altitude 9937 ft
05:14 Wind:164º@026 Knots Heading: 214º Ground Speed: 155 Knots Altitude 10029 f
t
05:29 Wind:158º@026 Knots Heading: 213º Ground Speed: 158 Knots Altitude 10000 ft
05:44 Wind:162º@021 Knots Heading: 245º Ground Speed: 171 Knots Altitude 10006 ft
05:56 Simulation Rate 1x
05:59 Wind:160º@023 Knots Heading: 243º Ground Speed: 170 Knots Altitude 10022 ft
06:14 Wind:167º@016 Knots Heading: 217º Ground Speed: 160 Knots Altitude 10013 ft
06:15 TOD
06:15 Fuel Weight: 1145 Lb
06:26 Gear Down: 143 Knots
06:26 Flaps:1 at 142 Knots
06:27 Flaps:2 at 142 Knots
06:29 Wind:120º@006 Knots Heading: 191º Ground Speed: 108 Knots Altitude 2470 ft
06:31 Flaps:4 at 105 Knots
06:34 TouchDown:Rate -124 ft/min Speed: 58 Knots
06:35 Flaps:0 at 39 Knots
06:35 Land
06:35 Wind:335º@010 Knots
06:35 Heading: 139º
06:35 Flight Duration: 07:36
06:35 Landing Weight: 22577 Lbs
06:35 POS N53º 53´ 54´´ W166º 32´ 38´´
06:36 Parking brakes on
06:36 Block to Block Duratio
n: 07:37
06:36 Final Fuel: 1044 Lbls
06:36 Spent Fuel: 3619 Lbls
06:36 Flight Length: 1221 NM
06:36 TOD Land Length: 43 NM