TeddyKiller Posted July 15, 2010 Share Posted July 15, 2010 I have a .txt file, and this displays whats in it.. $file = $_SERVER['DOCUMENT_ROOT'] . "/mirketh/chat.txt"; //Path to your *.txt file $contents = file($file); $string = implode($contents); echo $string; but what I want is to display the last XX lines of the file, and resave the file with those lines. Any help? thanks Quote Link to comment https://forums.phpfreaks.com/topic/207890-display-and-save-last-xx-lines-of-a-txt-file/ Share on other sites More sharing options...
Pikachu2000 Posted July 15, 2010 Share Posted July 15, 2010 Count the array elements before imploding $contents, then echo the 2 elements you want. As far as saving it, it's isn't real clear what you mean. Do you want to save only those 2 lines, or . . . ? Quote Link to comment https://forums.phpfreaks.com/topic/207890-display-and-save-last-xx-lines-of-a-txt-file/#findComment-1086760 Share on other sites More sharing options...
TeddyKiller Posted July 15, 2010 Author Share Posted July 15, 2010 Count the array elements before imploding $contents, then echo the 2 elements you want. As far as saving it, it's isn't real clear what you mean. Do you want to save only those 2 lines, or . . . ? I don't understand? I mean, its messages.. so it could be. Hi wre grgd hrijg sfdg rjire hergreihre ghrjgih or just one word, but all with a <br /> at the end... what would count as an element. What i'm saying is, if there is 11 lines, only the last XX lines are in the .txt, the one before gets removed.. so it just basically overwrites the file only savin the XX Also, when I say XX, i mean.. -certain number- so, 1, or 10, or 1000, whatever I choose. Just want to know how I would do it. Quote Link to comment https://forums.phpfreaks.com/topic/207890-display-and-save-last-xx-lines-of-a-txt-file/#findComment-1086765 Share on other sites More sharing options...
AbraCadaver Posted July 15, 2010 Share Posted July 15, 2010 If I understand correctly, this should work: $xx = 100; $contents = file($file); $lastxx = array_slice($contents, -$xx); file_put_contents($file, implode(PHP_EOL, $lastxx)); echo implode($lastxx); Quote Link to comment https://forums.phpfreaks.com/topic/207890-display-and-save-last-xx-lines-of-a-txt-file/#findComment-1086770 Share on other sites More sharing options...
Pikachu2000 Posted July 15, 2010 Share Posted July 15, 2010 file() reads each line of a file into an array element. If the file has 100 lines, the array will have elements 0-99, each containing one line. If you want to see how your file is loaded into the array, you can throw a echo '<pre>'; print_r($contents); echo'</pre>'; in to your script above, then you'll be able to determine how you want to process its contents. Quote Link to comment https://forums.phpfreaks.com/topic/207890-display-and-save-last-xx-lines-of-a-txt-file/#findComment-1086773 Share on other sites More sharing options...
TeddyKiller Posted July 15, 2010 Author Share Posted July 15, 2010 If I understand correctly, this should work: $xx = 100; $contents = file($file); $lastxx = array_slice($contents, -$xx); file_put_contents($file, implode(PHP_EOL, $lastxx)); echo implode($lastxx); It doesn't keep the last 100 lines.. it only keeps the last 1. It removes them.. i think one by one.. and boom.. only one remains, and yeah... it is working as words, instead of lines Quote Link to comment https://forums.phpfreaks.com/topic/207890-display-and-save-last-xx-lines-of-a-txt-file/#findComment-1086788 Share on other sites More sharing options...
TeddyKiller Posted July 16, 2010 Author Share Posted July 16, 2010 Any help... still can't figure it out, or find anything on the web. Quote Link to comment https://forums.phpfreaks.com/topic/207890-display-and-save-last-xx-lines-of-a-txt-file/#findComment-1087019 Share on other sites More sharing options...
kenrbnsn Posted July 16, 2010 Share Posted July 16, 2010 The code you said doesn't work, should work perfectly. Can you show us why you think it doesn't work. An example of the original file, your code, and the resultant file. Ken Quote Link to comment https://forums.phpfreaks.com/topic/207890-display-and-save-last-xx-lines-of-a-txt-file/#findComment-1087029 Share on other sites More sharing options...
meltingpoint Posted July 16, 2010 Share Posted July 16, 2010 Give this a whirl (tested and works for me- IF your text file contains only 1 message per line) <?php $record_count =1; // $openedfile =fopen("msg.txt", "r"); if(!$openedfile) { echo "Sorry- Could not open file"; exit; } // $hold[$record_count] = explode("|", trim(fgets($openedfile))); while(!feof($openedfile)) { $record_count++; $hold[$record_count] = explode("|", trim(fgets($openedfile))); } fclose($openedfile); //---------Now we can reverse the order of the array to display----------------------------- for ($i = 1; $i <= count($hold)-1; $i++) { $k = count($hold)-$i; // //-------------------------------Now declare variable to display------------------- // $element1 = $hold[$k][0]; if($k <= 100) { echo $element1; echo "<br>"; } } ?> So if your Text file looked like this; This is message #1 This is message #2 This is message #3 This is message #4 Then it will output exactly that but in reverse order (last post displayed first). This is message #4 This is message #3 This is message #2 This is message #1 ? Save. Save to what as it was already pulled from your saved .txt file. Quote Link to comment https://forums.phpfreaks.com/topic/207890-display-and-save-last-xx-lines-of-a-txt-file/#findComment-1087033 Share on other sites More sharing options...
kenrbnsn Posted July 16, 2010 Share Posted July 16, 2010 That's not what the OP asked for. He asked to display the last X lines of a file and write those lines back to the original file (i.e. shrinking the file). Ken Quote Link to comment https://forums.phpfreaks.com/topic/207890-display-and-save-last-xx-lines-of-a-txt-file/#findComment-1087049 Share on other sites More sharing options...
meltingpoint Posted July 16, 2010 Share Posted July 16, 2010 Your correct Ken- I hadn't seen a solution to the first half of displaying it either. I will complete it when I return from an errand. Cheers- Quote Link to comment https://forums.phpfreaks.com/topic/207890-display-and-save-last-xx-lines-of-a-txt-file/#findComment-1087083 Share on other sites More sharing options...
litebearer Posted July 16, 2010 Share Posted July 16, 2010 Perhaps... $file = whatever; $array1 = file_get_contents($file); $x = count($array1); $y = the number you want to keep $z = $x - $y; $i = 0; while($i<$z) { $junk = array_shift($array1); $i ++; } file_put_contents($file,$array1); $junk1 = implode($array1); echo $junk1; Quote Link to comment https://forums.phpfreaks.com/topic/207890-display-and-save-last-xx-lines-of-a-txt-file/#findComment-1087101 Share on other sites More sharing options...
kenrbnsn Posted July 16, 2010 Share Posted July 16, 2010 This works fine: <?php $cur = file('test100a.txt'); $n = rand(50,75); echo "Getting the last $n lines\n"; $n *= -1; $last = array_slice($cur,$n); file_put_contents('test100a.txt',implode("",$last)); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/207890-display-and-save-last-xx-lines-of-a-txt-file/#findComment-1087106 Share on other sites More sharing options...
Pikachu2000 Posted July 16, 2010 Share Posted July 16, 2010 I may have misinterpreted, but I think perhaps we're overlooking something important here. I suspect the linefeeds are being stripped when the file is initially saved, based on this statement: It doesn't keep the last 100 lines.. it only keeps the last 1. It removes them.. i think one by one.. and boom.. only one remains, and yeah... it is working as words, instead of lines Quote Link to comment https://forums.phpfreaks.com/topic/207890-display-and-save-last-xx-lines-of-a-txt-file/#findComment-1087108 Share on other sites More sharing options...
TeddyKiller Posted July 16, 2010 Author Share Posted July 16, 2010 This works fine: <?php $cur = file('test100a.txt'); $n = rand(50,75); echo "Getting the last $n lines\n"; $n *= -1; $last = array_slice($cur,$n); file_put_contents('test100a.txt',implode("",$last)); ?> Ken Can you explain this code please, so i can understand it. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/207890-display-and-save-last-xx-lines-of-a-txt-file/#findComment-1087202 Share on other sites More sharing options...
AbraCadaver Posted July 16, 2010 Share Posted July 16, 2010 Any help... still can't figure it out, or find anything on the web. What I posted works if the file is structured the way that it needs to for your original code to work. Post what Pikachu2000 suggested with print_r(). Quote Link to comment https://forums.phpfreaks.com/topic/207890-display-and-save-last-xx-lines-of-a-txt-file/#findComment-1087220 Share on other sites More sharing options...
kenrbnsn Posted July 16, 2010 Share Posted July 16, 2010 Fully commented code: <?php $cur = file('test100a.txt'); // read the file into the array named $cur $n = rand(50,75); // just a random number echo "Getting the last $n lines\n"; // debug code $n *= -1; // make the number negative $last = array_slice($cur,$n); // use array_slice function to get the last $n lines file_put_contents('test100a.txt',implode("",$last)); // write those lines back to the file overwriting whatever was there before ?> See array_slice for an explanation of using the negative number. Ken Quote Link to comment https://forums.phpfreaks.com/topic/207890-display-and-save-last-xx-lines-of-a-txt-file/#findComment-1087238 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.