I have the following snippet of code that I am trying to use to read a file that can have some lines repeated two or more times. The goal of this script is to only write unique lines (no duplicates) but for some reason it appears that it is not detecting equality. Any thoughts?
$handle = @fopen("Old.csv", "r");
$new = @fopen("New.csv", "w");
$last_line = null;
if ($handle && $new) {
while (($buffer = fgets($handle, 4096)) !== false) {
if( $last_line != $buffer ) fwrite( $new, $buffer );
$last_line = $buffer;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail
";
}
fclose($handle);
fclose($new);
}
Here is an example of "Old.csv"
apple
apple
orange
grapes
grapes
grapes
"New.csv" should be:
apple
orange
grapes
But it ends up being an exact copy of "Old.csv".