string replace in a large file with php

后端 未结 6 1910
借酒劲吻你
借酒劲吻你 2020-12-09 12:41

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

6条回答
  •  青春惊慌失措
    2020-12-09 13:18

    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.

    In any case, the sed (Stream Editor) command is what you are looking for:

    sed s/search/replace oldfilename > newfilename
    

    If you need case-insensitivity:

    sed s/search/replace/i oldfilename > newfilename
    

    If you need this to perform dynamically within PHP, you can use passthru():

    $output = passthru("sed s/$search/$replace $oldfilename > $newfilename");
    

提交回复
热议问题