Jump to content

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


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 :)

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.