How to remove multiple lines in txt file using fwrite/fopen method? This is my .txt file
One
Two
Three
One
Four
Five
Three
Eight
I want to remove those repeated lines (One & Three)
Help me, thanks!
Started on the old forum in March 2013 · view the original in the archive · old links redirect here
How to remove multiple lines in txt file using fwrite/fopen method? This is my .txt file
One
Two
Three
One
Four
Five
Three
Eight
I want to remove those repeated lines (One & Three)
Help me, thanks!
I don't really understand what you want to achieve but...
$chop_lines = array(4, 7);
$file = "path/to/file.txt";
$data = file_get_contents($file);
$data_array = explode("\n", $data);
foreach ($chop_lines as $line)
{
$line = (int) $line - 1;
if(isset($data_array[$line])) unset($data_array[$line]);
}
$data = implode("\n", $data_array);
file_put_contents($file);
If on the other hand u wanna keep only unique entries, u could probably do something like...
$file = "path/to/file.txt";
$data = file_get_contents($file);
$data_array = explode("\n", $data);
$data_array = array_unique($data_array);
$data = implode("\n", $data_array);
file_put_contents($file);