Jump to content

Display and save last XX lines of a .txt file


TeddyKiller

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.