用php替换大文件中的字符串 [英] string replace in a large file with php
本文介绍了用php替换大文件中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试用PHP替换整个文件的字符串.我的文件超过100MB,因此我必须逐行浏览,并且不能使用file_get_contents()
.有一个好的解决方案吗?
I am trying to do a string replace for entire file in PHP. My file is over 100MB so I have to go line by line and can not use file_get_contents()
. Is there a good solution to this?
推荐答案
如果您不需要使用PHP,我强烈建议您在命令行中执行类似的操作.到目前为止,它是完成这项工作的最佳工具,并且更易于使用.
If you aren't required to use PHP, I would highly recommend performing stuff like this from the command line. It's by far the best tool for the job, and much easier to use.
无论如何,您就是 sed
(流编辑器)命令寻找:
In any case, the sed
(Stream Editor) command is what you are looking for:
sed s/search/replace oldfilename > newfilename
如果您不区分大小写:
sed s/search/replace/i oldfilename > newfilename
如果需要此功能以在PHP中动态执行,则可以使用passthru()
:
$output = passthru("sed s/$search/$replace $oldfilename > $newfilename");
这篇关于用php替换大文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文