Zubrag.com :: Forum

Programmers Lounge => Tips and Tricks => Topic started by: zubrag on November 02, 2007, 03:41:04 AM



Title: Remove duplicate lines from a file
Post by: zubrag on November 02, 2007, 03:41:04 AM
Remove duplicate lines from a file.

<?php

// filename
$filename = 'cars.txt';

$text = array_unique(file($filename));

$f = @fopen($filename,'w+');
if ($f) {
  fputs($f, join('',$text));
  fclose($f);
}

?>
Change cars.txt above to target file name. Result will be saved into the same file.
If you want to same result to another file replace $f = @fopen($filename,'w+');  with $f = @fopen('result-file-name.txt','w+');


Title: Re: Remove duplicate lines from a file
Post by: YellowNick on February 02, 2012, 10:15:23 AM
Yes this is just what I need. I have a huge file with lots of duplicates.