trauch Posted December 22, 2008 Share Posted December 22, 2008 So as to not confuse anyone regarding my progress I have started a new thread. I'm working on a script that will perform a few tasks 1) Load multiple text files each of which contains a line of numbers 2) In each text file remove white space and then add all lines 3) Append the contents of all of these text files to another text file 4) Add all numbers in the resulting text file 5) Save that resulting number as a new text file whose name is generated as the current date. (ie 2008-12-22.txt) I've got some messy code below but would appreciate any assistance. <?php $data1 = file('textfile1.txt'); $data1 = str_replace(' ','',$data1); echo 'sum($data1) = ' . array_sum($data1) . "\n"; $sum = array_sum($data1) . "\n"; $data2 = file('textfile2.txt'); $data2 = str_replace(' ','',$data2); echo 'sum($data2) = ' . array_sum($data2) . "\n"; $sum2 = array_sum($data2) . "\n"; $data3 = file('textfile3.txt'); $data3 = str_replace(' ','',$data3); echo 'sum($data3) = ' . array_sum($data3) . "\n"; $sum3 = array_sum($data3) . "\n"; $fp = fopen('textmaster.txt', 'a'); fwrite($fp, $sum); fwrite($fp, $sum2); fwrite($fp, $sum3); fclose($fp); ?> Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted December 22, 2008 Share Posted December 22, 2008 There are a few ways of doing it, but this is how I would do it: <?php // Make an array of the filenames to sum $fileNames = array('textfile1.txt', 'textfile2.txt', 'textfile3.txt'); // Open the file and zero out the net sum $fp = fopen('textmaster.txt', 'a'); $netSum = 0; // Add the numbers and append to the file foreach($fileNames as &$fileName) { $content = str_replace(' ', '', file($fileName)); // Perhaps use preg_replace to remove ALL whitespace instead of just spaces? $sum = array_sum($content); echo 'Sum of "' . $fileName . ': ' . $sum; $netSum += $sum; fwrite($fp, $sum); } // Clean up fclose($fp); // Create the day file file_put_contents(date('Y-n-j') . '.txt', $netSum); ?> I didn't test it, but you get the gist of it. Quote Link to comment Share on other sites More sharing options...
trauch Posted December 22, 2008 Author Share Posted December 22, 2008 this looks really promising but I got an error on line 10 Parse error: syntax error, unexpected '&', expecting T_VARIABLE or '$' Line 10 says: foreach($fileNames as &$fileName) { Any ideas? Btw, Im using php4. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted December 22, 2008 Share Posted December 22, 2008 Ah, php4 doesn't support foreach references, you'll need to remove the &. There may be some other issues as I can't recall if any functions I used are php5 only. All the more incentive for you to figure out what the code does before using it! Quote Link to comment Share on other sites More sharing options...
trauch Posted December 22, 2008 Author Share Posted December 22, 2008 I removed the & and it seems to be adding the individual text files. Unfortunately php4 also does not recognize file_put_contents. Do you know how to do this with fwrite or some other way with php4? Quote Link to comment Share on other sites More sharing options...
trauch Posted December 22, 2008 Author Share Posted December 22, 2008 With the help of genericnumber1 I'm almost there but I would appreciate any input. Since Im using php4 I cant use file_put_contents and am still trying to figure out how to save the file as the current date (2008-12-22.txt). I know I have to use fwrite but am not sure how to do this. Here's what I have thus far. <?php // Make an array of the filenames to sum $fileNames = array('textfile.txt', 'textfile2.txt', 'textfile3.txt'); // Open the file and zero out the net sum $fp = fopen('textmaster.txt', 'a'); $netSum = 0; // Add the numbers and append to the file foreach($fileNames as $fileName) { $content = str_replace(' ', '', file($fileName)); $sum = array_sum($content); echo 'Sum of "' . $fileName . ': ' . $sum; $netSum += $sum; fwrite($fp, $sum); } // Clean up fclose($fp); // Create the day file fwrite(date('Y-n-j') . '.txt', $netSum); ?> Quote Link to comment Share on other sites More sharing options...
Lamez Posted December 22, 2008 Share Posted December 22, 2008 why don't you just update your PHP? Quote Link to comment Share on other sites More sharing options...
trauch Posted December 22, 2008 Author Share Posted December 22, 2008 I wish I could. Im on a shared server and my host has control over upgrading so... Quote Link to comment Share on other sites More sharing options...
Lamez Posted December 22, 2008 Share Posted December 22, 2008 I see, maybe this will help: http://www.phpied.com/file_get_contents-for-php4/ Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted December 22, 2008 Share Posted December 22, 2008 http://us2.php.net/file_put_contents I promise the answer is here look and you shall find! Quote Link to comment Share on other sites More sharing options...
trauch Posted December 22, 2008 Author Share Posted December 22, 2008 all I can say is WOW! Thanks 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.