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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.