Cesar Bielich
Cesar Bielich

Reputation: 4945

Remove duplicate data in a text file with php

I am in need to open a text file (file.txt) which contains data in the following format

ai
bt
bt
gh
ai
gh
lo
ki
ki
lo

ultimately I want to remove all the duplicate lines so only one of each data remains. So the result would look like this

ai
bt
gh
lo
ki

any help with this would be awesome

Upvotes: 7

Views: 18945

Answers (4)

Sam Martini
Sam Martini

Reputation: 1

$lines = file_get_contents('file.txt');
$lines = explode('\n', $lines);
$lines = array_unique($lines);
$lines = implode('\n', $lines);
file_put_contents('file.txt', $lines);

Upvotes: 0

taoufiqaitali
taoufiqaitali

Reputation: 480

This might work:

$txt = implode('\n',array_unique(explode('\n', $txt)));

Upvotes: 0

wassup
wassup

Reputation: 2503

This should do the trick:

$lines = file('file.txt');
$lines = array_unique($lines);

file() reads the file and puts every line in an array.

array_unique() removes duplicate elements from the array.

Also, to put everything back into the file:

file_put_contents('file.txt', implode($lines));

Upvotes: 26

David Müller
David Müller

Reputation: 5351

Take the php function file() to read the file. You get an array of lines from your file. After that, take array_unique to kick out the duplicates.

In the end, you will have something like

$lines = array_unique(file("your_file.txt"));

Upvotes: 2

Related Questions