This should do the trick:
$lines = file('file.txt'); $lines = array_unique($lines);
file() reads the file and puts each line in an array.
array_unique() removes duplicate elements from an array.
In addition, to return everything to a file:
file_put_contents('file.txt', implode($lines));
source share