从文本文件中删除一些字符串

时间:2018-05-04 17:15:15

标签: php file text

有大约3 GB的文本文件。我需要从中删除一些字符串,但我不确定我的方法是否正常。我做了下一步: - 从doc中读取echo字符串 - 找到要删除的字符串 - 得到2个大量:要保存的字符串和要删除的字符串

接下来的步骤必须是什么?是的,对于小文档来说这个任务看起来很容易,但是巨型文件存在更多问题。

1 个答案:

答案 0 :(得分:0)

if( $fh = fopen("file.txt", "r") ){ 
        $left='';

        while (!feof($fh)) {// read the file
        $temp = fread($fh);  
        $fgetslines = explode("\n",$temp);
        $fgetslines[0]=$left.$fgetslines[0];

             if(!feof($fh) )$left = array_pop($lines);           
             foreach($fgetslines as $k => $line){
                   //This is where you can build your check for the strings you want to remove 
                   //if statement or switch, which ever makes sence with your current logic.
                   //After excluding your strings from the temp file 
                   //overwrite your original file with the temp file of proper strings that you want.
              }
        }
}
fclose($fh);

我认为这就是你要找的东西。