0

Say for example you had a file like this:

1, 2, 3, 4

apples, oranges, bananas

cats, dogs, snakes, hamsters

And say if you wanted to append it so that the second line read: apples, oranges, bananas, mangos

How would you do this. When learning PHP I couldn't find any tutorials that explain how to do this so I am a bit lost. Thanks.

puckloe
  • 7
  • 4
  • 1
    Read it line by line, write each line to a new file, when you see the line you want to amend, amend it before writing it. – RiggsFolly Feb 27 '22 at 08:58
  • You can also read the whole file into an array of lines, find the one you want, amend it. Then write the array back to the file. but this may fail on large files as there wont be enough memory to cope – RiggsFolly Feb 27 '22 at 09:00
  • Your life would be easier if you stored it e.g. as JSON or any other structured format. Otherwise, unless each line has a key, how will you identify which line you need to update when your data changes? – Markus AO Feb 27 '22 at 12:36

0 Answers0