I have a text file that I am trying to remove duplicate lines.
Example text file:
new featuredProduct('', '21640'),
new featuredProduct('', '24664'),
new featuredProduct('', '22142'),
new featuredProduct('', '22142'),
new featuredProduct('', '22142'),
new featuredProduct('', '22142'),
new featuredProduct('', '22142'),
PHP code I tried:
$lines = file('textfile.txt');
$lines = array_unique($lines);
file_put_contents('textfile.txt', implode($lines));
The PHP file is called duplicates.php, and the text file is in the same directory. I would like to leave only:
new featuredProduct('', '21640'),
new featuredProduct('', '24664'),
new featuredProduct('', '22142'),
The file function tries to read the file in the $ lines array and then array_unique () to remove duplicate entries. Then postpone the filtered results in the same file.
source
share