How to delete a line from the file with php?

后端 未结 9 1463
北荒
北荒 2020-11-27 19:36

I have a file named $dir and a string named $line, I know that this string is a complete line of that file but I don\'t know its line number and I

9条回答
  •  無奈伤痛
    2020-11-27 20:13

    It can be solved without the use of awk:

    function remove_line($file, $remove) {
        $lines = file($file, FILE_IGNORE_NEW_LINES);
        foreach($lines as $key => $line) {
            if($line === $remove) unset($lines[$key]);
        }
        $data = implode(PHP_EOL, $lines);
        file_put_contents($file, $data);
    }
    

提交回复
热议问题