PHP read a large file line by line and string replace -
i'd read large file line line, perform string replacement , save changes file, iow rewriting 1 line @ time. there simple solution in php/ unix? easiest way came on mind write lines new file , replace old one, it's not elegant.
i think there're 2 option
- use memory
read, replace store replaced string memory, once done, overwrite source file.
- use tmp file
read & replace string write every line tmp file, once done, replace original file tmp file
the #1 more effective bcuz io expensive, use if have vast memory or processing file not big.
the #2 bit slow quite stable on large file.
of course, may combine both way writing replaced string by chunk of lines file (instead of by line)
there're simplest, elegant ways can think out.
please correct me if wrong.
Comments
Post a Comment