user965748
user965748

Reputation: 2387

PHP read a large file line by line and string replace

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.

Upvotes: 0

Views: 916

Answers (2)

DQM
DQM

Reputation: 552

I think there're only 2 options

  1. Use memory

Read, replace then store the replaced string into memory, once done, overwrite the source file.

  1. 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.

Upvotes: 1

user965748
user965748

Reputation: 2387

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

Upvotes: 0

Related Questions