Jump to content

[SOLVED] Remove headers in txt file


jschofield

Recommended Posts

Hello All,

I am going to try to create a php script to remove the headers I have in a file. The problem is I am not sure on how to start off to remove these headers. Any help would be great as my boss is all over me to get this done. Data: is my headers in my files.[Thanks everyone.

 

Data:

 

0qqnam00.p ORTING SCHOOL DISTRICT Time: 12:36 PM Date: 03/27/07

03.06.06.00.00-010008 Name Report For Staff PAGE:        1

 

NAME KEY    LFM NAME                            PRIME PHONE# ADDRESS

 


<?php

 

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

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

$logfilename = "C:/SMDIM/Logs/Orting_Staff_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/All Staff.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");

}

 

?>

Link to comment
Share on other sites

Not sure how to easily do this in php, BUT if your .php scripts run on a Linux/*nix machine (I see it's windows based in the code -- however, don't know if windows cmd line can perform the following), you can use *nix's grep -A NUM syntax from the exec() function.  the -A NUM tells grep to display (or pipe) all of the text after a certain line.  There is a windows version of grep, http://www.wingrep.com/download.htm.  With that installed you can prepare the file before trying to read it into php with AT/CRON jobs and then do your thing in php. 

 

Now, if you solely want to use php, then you're going to have to store the file into an array of some sorts (maybe) and then parse out only the data you need.  Maybe something I've provided will help, maybe not... ;-) 

 

ps:  the exec() function can be ref'd here: http://us2.php.net/function.exec

Link to comment
Share on other sites

Hi, I got a problem

on windows web servers when trying to use text files in UNICODE

instead of ANSI.

 

Do you know how to use/display  this text files

as in Linux web servers?

 

Beacuse when using locally in Windows xp has no problem.

 

Thanks

Lisa

lisawebs@yahoo.com

 

 

Not sure how to easily do this in php, BUT if your .php scripts run on a Linux/*nix machine (I see it's windows based in the code -- however, don't know if windows cmd line can perform the following), you can use *nix's grep -A NUM syntax from the exec() function.  the -A NUM tells grep to display (or pipe) all of the text after a certain line.  There is a windows version of grep, http://www.wingrep.com/download.htm.  With that installed you can prepare the file before trying to read it into php with AT/CRON jobs and then do your thing in php. 

 

Now, if you solely want to use php, then you're going to have to store the file into an array of some sorts (maybe) and then parse out only the data you need.  Maybe something I've provided will help, maybe not... ;-) 

 

ps:  the exec() function can be ref'd here: http://us2.php.net/function.exec

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.