Jump to content

[SOLVED] Remove header lines from a txt file


jschofield

Recommended Posts

Hello fellow phpers,

I am having trouble removing header lines that I have in a txt file. The file is a tabbed delimited comma. Below is some of my code but I'm not sure if it will even work. Any help or different ideas on how to do this will be a great help to me. Thanks all.

 

CODE:

 

while ($row = fgetcsv($inputfp, 1000, "\t")){

$row = str_replace("\t", ",", $row);

if($row[0] == ""){ continue;}

if(trim($row[0] == "Ref:  ATT.515D")){ continue;}

if($row[0] == "Date:  2/21/07  "){continue;}

if(trim($row[0] == "Time:  9:40:52 ")){continue;}

if(trim($row[0] == "-------------")){continue;}

if(trim($row[0] == "")){continue;}

if(trim($row[0] == "Student #")){continue;}

if(trim($row[0] == "-------------")){continue;}

 

TEXT FILE HEADERS:

 

Ref:  ATT.515D                        LIVINGSTON SCHOOL DISTRICT                                                          Page    1

Date:  2/21/07                  50    LIVINGSTON HIGH SCHOOL

Time:  9:40:52                        Student Absences from  2/13/07 to  2/13/07    Absence Types: (Au) (Ae)      (T )

------------------------------------------------------------------------------------------------------------------------------------

                                            Homeroom        Homeroom                                              Daily

Student # Student Name                  Cl  Teacher        Number  01 02 03 04 05 06 07 08 09                    Reason  Date

------------------------------------------------------------------------------------------------------------------------------------

so, you want to remove the header from the text file... do you simply want to retrieve the data from this text file AFTER the header? or do you want to physically delete the first three lines from the file and move the rest of the text up to the top?

Either would actually work. I want to pull information below the headers that I need to create a csv file. Below is my whole script and some of the data file. The data I need is student #, and the 01-09. Hope this helps. Thanks again for everything!!!!

CODE:

 

<?php

 

$inputfile = "C:/SMDIM/Data/attendanceinfo.txt";

$outputfile = "C:/SMDIM/Data/Livingston_Attendance_Data.csv";

$logfilename = "C:/SMDIM/Logs/Livingston_Attendance_Log.txt";

include'C:/SMDIM/Translators/importlibrary.inc.php';

 

$count = 0;

 

//open the file

if(!$outfilefp = fopen($outputfile, "w")) {

wlogdie("Failed to open $outputfile");

}else{

wlog("Opened $outputfile for writing");

}

 

if(!$inputfp = fopen($inputfile, "r")) {

wlogdie("Failed to open $inputfile");

}else{

wlog("Opened $inputfile for reading");

}

 

//remove header lines

while ($row = fgetcsv($inputfp, 1000, "\t")){

$row = str_replace("\t", ",", $row);

if($row[0] == ""){ continue;}

if(trim($row[0] == "Ref:  ATT.515D")){ continue;}

if($row[0] == "Date:  2/21/07  "){continue;}

if(trim($row[0] == "Time:  9:40:52 ")){continue;}

if(trim($row[0] == "-------------")){continue;}

if(trim($row[0] == "")){continue;}

if(trim($row[0] == "Student #")){continue;}

if(trim($row[0] == "-------------")){continue;}

$temp  = array($row[0], $row[1], $row[2], $row[3], $row[4], $row[6]);

$count++;

writeLine($outfilefp, $row, $temp);

}

if($count !== '0') {

wlog("finished the import with a total of $count records");

}else{

wlog("Failed to import data");

}

 

?>

 

DATA:

 

Ref:  ATT.515D                        LIVINGSTON SCHOOL DISTRICT                                                          Page    1

Date:  2/21/07                  50    LIVINGSTON HIGH SCHOOL

Time:  9:40:52                        Student Absences from  2/13/07 to  2/13/07    Absence Types: (Au) (Ae)      (T )

------------------------------------------------------------------------------------------------------------------------------------

                                            Homeroom        Homeroom                                              Daily

Student # Student Name                  Cl  Teacher        Number  01 02 03 04 05 06 07 08 09                    Reason  Date

------------------------------------------------------------------------------------------------------------------------------------

    83375 ABEL, ALEXANDRA B.            11  JEDWABNIK      A101    T    A                                        T    2/13/07

    71323 ABOUDA, NADA                  12  TEJEDA, J      A103                          A  A                      D    2/13/07

  105006 ACKERMANN, JEFFREY            09  TRACY          AGYM1                        A  A                      F    2/13/07

    73368 ADIGUZEL, DILSUZ R.            12  LOVI            B222    A                                              T    2/13/07

    73005 AIELLO, ANTHONY                12  FABRIZZIO      E218    T              A  A  A  A                      T    2/13/07

well, to skip the first three lines all you gotta do is:

<?php
        while($row = fgetcsv($inputfp, 1000, "\t")){
                if($row[0]){ continue; }
                if($row[1]){ continue; }
                if($row[2]){ continue; }

                //do whatever you do to the rest
                //of the document here
        }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.