jschofield Posted March 12, 2007 Share Posted March 12, 2007 Hello Fellow phpers, I have been having trouble removeing two header lines at the top of my txt file. You will see in my code the different things I have tried but with no luck. Below I posted my script and data. I am doing a few other things in my script as you will notice but this is the only thing I am having troble with. Any help would be great. Thanks everyone for the help. Johnnie DATA: number_of_records=97 last24hours 20380,3/7/2007,E 20302,3/7/2007,D 20743,3/7/2007,E 20380,3/5/2007,E 20380,3/7/2007,E SCRIPT: <?php $inputfile = "C:/SMDIM/Data/HSattendance.txt"; $outputfile = "C:/SMDIM/Data/PlainviewHS_Attendance_Data.csv"; $logfilename = "C:/SMDIM/Logs/LPlainviewHS_Attendance_Log.txt"; //include log script include'C:/SMDIM/Translators/importlibrary.inc.php'; $lines = file("C:/SMDIM/Data/HSattendance.txt"); //$fieldlengths = array(21, 11); $count = 0; //open the file if(!$outfilefp = fopen($outputfile, "w")) { wlogdie("Failed to open $outputfile"); }else{ wlog("Opened $outputfile for writing"); } //read file if(!$inputfp = fopen($inputfile, "r")) { wlogdie("Failed to open $inputfile"); }else{ wlog("Opened $inputfile for reading"); } //remove header lines while ($row = fgets($inputfp)){ //if($row == "0 < 0"){ continue; } //$vals = fixedlenread($row, $fieldlengths); foreach ($lines as $lines_num => $line) { if(is_numeric($line)$to_wrote . =$line); { $count++; writeLine($outfilefp, $lines); } if($count !== '0') { wlog("finished the import with a total of $count records"); }else{ wlog("Failed to import data"); } } ?> Link to comment https://forums.phpfreaks.com/topic/42356-solved-remove-text-header-in-text-file/ Share on other sites More sharing options...
monk.e.boy Posted March 12, 2007 Share Posted March 12, 2007 Read the input file into an array, then do: <php $line_number = 0; foreach( $lines as $line ) { if( $line_number > 2 ) write_stuff_to_outputfile(); $line_number++; } ?> Hows that? monk.e.boy Link to comment https://forums.phpfreaks.com/topic/42356-solved-remove-text-header-in-text-file/#findComment-205456 Share on other sites More sharing options...
monk.e.boy Posted March 12, 2007 Share Posted March 12, 2007 look up file_get_contents You may need to do: $contents = file_get_contents( 'my_file.txt' ); $lines = split( "\n", $contents ); monk.e.boy Link to comment https://forums.phpfreaks.com/topic/42356-solved-remove-text-header-in-text-file/#findComment-205458 Share on other sites More sharing options...
jschofield Posted March 12, 2007 Author Share Posted March 12, 2007 Hey Monkey boy, I tried doing it a few different options that you suggested but I think I might be doing it in the wrong order. I copied what Ihave been doing below. What do you think..wrong order? Thanks again for your help..you have saved on this!! <?php $inputfile = "C:/SMDIM/Data/HSattendance.txt"; $outputfile = "C:/SMDIM/Data/PlainviewHS_Attendance_Data.csv"; $logfilename = "C:/SMDIM/Logs/LPlainviewHS_Attendance_Log.txt"; //include log script include'C:/SMDIM/Translators/importlibrary.inc.php'; $count = 0; $line_number = 0; //open the file if(!$outfilefp = fopen($outputfile, "w")) { wlogdie("Failed to open $outputfile"); }else{ wlog("Opened $outputfile for writing"); } //read file if(!$inputfp = fopen($inputfile, "r")) { wlogdie("Failed to open $inputfile"); }else{ wlog("Opened $inputfile for reading"); } { $contents = file_get_contents( "C:/SMDIM/Data/HSattendance.txt", FALSE, NULL,0,0 ); $lines = split("/n", $contents ); //if( $line_number > 2 ) //write_stuff_to_outputfile(); //$line_number++; } //remove header lines while ($row = fgets($inputfp)){ $count++; writeLine($outfilefp, $lines); } if($count !== '0') { wlog("finished the import with a total of $count records"); }else{ wlog("Failed to import data"); } ?> Link to comment https://forums.phpfreaks.com/topic/42356-solved-remove-text-header-in-text-file/#findComment-205508 Share on other sites More sharing options...
monk.e.boy Posted March 13, 2007 Share Posted March 13, 2007 <?php $inputfile = "C:/SMDIM/Data/HSattendance.txt"; $outputfile = "C:/SMDIM/Data/PlainviewHS_Attendance_Data.csv"; $logfilename = "C:/SMDIM/Logs/LPlainviewHS_Attendance_Log.txt"; //include log script include'C:/SMDIM/Translators/importlibrary.inc.php'; $count = 0; $line_number = 0; //open the file if(!$outfilefp = fopen($outputfile, "a")) // <-- NOTE the 'a' { wlogdie("Failed to open $outputfile"); }else{ wlog("Opened $outputfile for writing"); } //read file if(!$inputfp = fopen($inputfile, "r")) { wlogdie("Failed to open $inputfile"); }else{ wlog("Opened $inputfile for reading"); } { $contents = file_get_contents( "C:/SMDIM/Data/HSattendance.txt", FALSE, NULL,0,0 ); $lines = split("/n", $contents ); for( $lines as $line ) { if( $line_number > 2 ) fwrite(outfilefp, $line.'\n'); // <-- I have appended a newline but this may be wrong. $line_number++; } if($line_number !== '0') { wlog("finished the import with a total of ". ($line_number-2) ." records"); }else{ wlog("Failed to import data"); } ?> try that monk.e.boy Link to comment https://forums.phpfreaks.com/topic/42356-solved-remove-text-header-in-text-file/#findComment-206086 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.