Jump to content

Open file, remove duplicates


quercus

Recommended Posts

Hi

 

I'm new to PHP ;-)

 

I need to open a file, read the lines (CDR) and remove duplicates.

 

It must be simple. This is what i have tried:

 

$in_file = fopen("files/cdr_file","r") or exit("Unable to open file!");

while(!feof($in_file))

{

echo fgets($in_file). "< br >";

}

 

Someone tells me to use file() instead of fopen(), and others say that file() has problems with files > 10Mb.

And how do I use array_unique()

 

My output needs to go into a new file.

 

Henrik

Link to comment
https://forums.phpfreaks.com/topic/82023-open-file-remove-duplicates/
Share on other sites

Anything you do will take forever with a 10mb file, but here is one way:

 

$buff = file_get_contents("files/cdr_file", "r") or exit("Unable to open file!");
$lines = explode("\n", $buff);
$lines = array_unique($lines);
$buff = implode("\n", $lines);
file_put_contents("files/cdr_file", $buff);

 

That should get everything from the file, make an array from tokens delimited by a new line (I don't know how they are separated, so mess with that if it is different), removes duplicates, and puts it all back in the same file.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.