quercus Posted December 17, 2007 Share Posted December 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
lemmin Posted December 17, 2007 Share Posted December 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.