I'd like to read a large file line by line, perform string replacement and save changes into the file, IOW rewriting 1 line at a time. Is there any simple solution in PHP/ Unix? The easiest way that came on my mind would be to write the lines into a new file and then replace the old one, but it's not elegant.
Asked
Active
Viewed 880 times
2 Answers
1
I think there're only 2 options
- Use memory
Read, replace then store the replaced string into memory, once done, overwrite the source file.
- Use tmp file
Read & replace string then write every line immediately to tmp file, once all done, replace original file by tmp file
The #1 will be more effective because IO is expensive, use it if you have vast memory or the processing file is not too big.
The #2 will be a bit slow but be quite stable even on large file.
Of course, you may combine both way by writing replaced string by chunk of lines to file (instead of just by line)
There're the simplest, most elegant ways I can think out.

Jason Aller
- 3,541
- 28
- 38
- 38

DQM
- 562
- 3
- 15
-
That's right for small files RAM, for large ones a temporary file – user965748 Apr 27 '15 at 11:58
0
It seems it's not such a bad solution to use the temporary file in most cases.
$f='data.txt';
$fh=fopen($f,'r+');
while (($l=fgets($fh))!==false) file_put_contents('tmp',clean($l),FILE_APPEND);
fclose($f);
unlink($f);
rename('tmp',$f);

user965748
- 2,227
- 4
- 22
- 30