holyearth Posted October 19, 2006 Share Posted October 19, 2006 Hello everyone, need some help...I have the following code:$process = fopen("hitcounter.txt", "r"); //opens txt file for startnum$startnum = fread($process,8192); //starting numberWhich opens hitcounter.txt and extracts data from it and saves it as $startnumHow can I make it extract the data (500 lines at a time) and DELETE the data it extracted.So, it will open hitcounter.txt pull the first 500 lines save it as $startnum and delete the 500 lines from hitcounter.txt (the ones it extracted). Quote Link to comment Share on other sites More sharing options...
holyearth Posted October 19, 2006 Author Share Posted October 19, 2006 *Bump*Anyone? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 19, 2006 Share Posted October 19, 2006 You're description is a little muddy. What do you mean by "saving the first 500 lines as $startnum"?Use the [url=http://www.php.net/file]file()[/url] function to read the whole file into an array. Start at entry 500 (which skips the first 500 lines) and write the rest back to "hitcounter.txt".[code]<?php$hc_file = file('hitcounter.txt');if (count($hc_file) < 500) echo "Less than 500 lines in the files";else { $fp = fopen('hitcounter.txt','w'); // open a new version of the file for write for ($i=500;$i<count($hc_file);$i++) fwrite($fp,$hc_file[$i]); fclose($fp);}?>[/code]BTW, please don't bump so quickly.Ken Quote Link to comment Share on other sites More sharing options...
craygo Posted October 19, 2006 Share Posted October 19, 2006 I think this is what you want.[code]<?php$file = "hitcounter.txt";$textfile = file($file);$total_lines = count($textfile);$lines = 500; // set the number of lines you want to use hereif($total_lines < $lines){echo "Less than $lines in this file";} else {// here are your first 500 lines put into an array called $startnum. can do what you like with them$startnum = array();for($first=0; $first<$lines; $first++){$startnum[] = $textfile[$first];}// Here we store the rest of the file into an array for re-writing$handle = fopen($file, "w");for($last=$lines; $last<$total_lines; $last++){fwrite($handle, $textfile[$last]);}fclose($handle);}?>[/code]Ray Quote Link to comment Share on other sites More sharing options...
holyearth Posted October 19, 2006 Author Share Posted October 19, 2006 I'm trying to open hitcounter.txt "pull" the first 500 lines as $somethingBUTI want the 500 lines that were "pulled" from hitcounter.txt to be "removed" while they are pulled.So, it should extract the 500 lines and save the file without those 500 lines. Quote Link to comment Share on other sites More sharing options...
craygo Posted October 19, 2006 Share Posted October 19, 2006 that's what the above code does.it takes the first 500 lines and stores them as $startnum which is an array. then it takes the rest of the data and writes it back without the first 500 lines.Ray Quote Link to comment Share on other sites More sharing options...
holyearth Posted October 19, 2006 Author Share Posted October 19, 2006 Ray,Got it thanks!Now, im trying to "output" (to the user) the 500 lines it saved as an array using:<? echo("$startnum"); ?>But it only shows: Array Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 19, 2006 Share Posted October 19, 2006 You have to loop through the array, echoing each line:[code]<?phpforeach($startnum as $line) echo nl2br($line);?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
craygo Posted October 19, 2006 Share Posted October 19, 2006 If you are using the code I gave you then add this line.[code]<?php// here are your first 500 lines put into an array called $startnum. can do what you like with them$startnum = array();for($first=0; $first<$lines; $first++){$startnum[] = $textfile[$first];echo $textfile[$first]."<br>";}?>[/code] Quote Link to comment Share on other sites More sharing options...
holyearth Posted October 19, 2006 Author Share Posted October 19, 2006 Thank you ken and craygo.I appreciate your time and help! God Bless! 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.