jschofield Posted April 2, 2007 Share Posted April 2, 2007 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"); } ?> Quote Link to comment Share on other sites More sharing options...
jguy Posted April 2, 2007 Share Posted April 2, 2007 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 Quote Link to comment Share on other sites More sharing options...
lisawebs Posted April 3, 2007 Share Posted April 3, 2007 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.