Jump to content

[SOLVED] Limiting a text file to a certain number of lines...?


Jpoel

Recommended Posts

Hi again ;)

 

Heres the deal.

 

I have a text file, which is constantly having things written to it

 

example:

 

the text file is in the form of:

 

,James:1:

,Kimberley:2:

,Bob:3:

,Joe:4:

,John:5:

 

every now and again, a new value is inserted into the end of the text file, example, Josh is added:

 

,James:1:

,Kimberley:2:

,Bob:3:

,Joe:4:

,John:5:

,Josh:6:

 

However, when this php script is used to add a new value to the end of the text file, and the length of the file is 30 lines long in length, what i want is for the php script to remove the first line, and add the new value to the end, to maintain the 30 line limit

 

Does anyone know how i could do this?

 

Thanks.

 

PS I use Fwrite and Fread to write to the text file.

 

 

 

You can use file() to read the file into an array, where each line is an entry in the array. If count() of array == 30 then array_shift() to remove the first entry, implode() the array back into a string, append your new line, rewrite the file.

You can use file() to read the file into an array, where each line is an entry in the array. If count() of array == 30 then array_shift() to remove the first entry, implode() the array back into a string, append your new line, rewrite the file.

 

 

thanks KrisNz, that did the job :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.