javaMan1995
javaMan1995

Reputation: 51

Sort and remove duplicates line by line in text file

So I have this text file that I wanted to sort. The contents of the text file looks something like this: I used "\r\n" at the end of each line when I was writing them in

Gere is the code that I used writing this:

$name = $_POST['name'];
$email = $_POST['email'];

$fileName = "GuestBook.txt";

  $fh = fopen($fileName, "a");
    fwrite($fh, $name);
    fwrite($fh, "----");
    fwrite($fh, $email . "\r\n");
  fclose($fh);

Opening txt file on the notepad it looks something like this:

aaa----aaa
ggg----ggg
sss----sss
www----www
ttt----ttt
ppp----ppp
ggg----ggg
zzz----zzz
www----www

Now I want it to display it sorted with duplicates removed:

aaa----aaa
ggg----ggg
ppp----ppp
sss----sss
ttt----ttt
www----www
zzz----zzz

This is what I did for sorting them:

$fileName = "GuestBook.txt";

$data = file_get_contents($fileName);
$split = explode("\n",$data);

sort($split);

$data = implode("\n",$split);

file_put_contents($fileName, $data);

This is what I have for removing duplicates

$fileName = "GuestBook.txt";

$lines = file($fileName);
$lines = array_unique($lines);

file_put_contents($fileName, implode("\n",$lines));

Upvotes: 1

Views: 1625

Answers (3)

AbraCadaver
AbraCadaver

Reputation: 78994

Load the file into an array, remove duplicates and then sort:

$lines = array_unique(file('/path/to/file.txt'));
sort($lines);

Depending on file structure you may use one or both of FILE_IGNORE_NEW_LINES and FILE_SKIP_EMPTY_LINES.

Then to save, if you don't use FILE_IGNORE_NEW_LINES (if so you have to add them):

file_put_contents('/path/to/file.txt', $lines);

Upvotes: 1

Rizier123
Rizier123

Reputation: 59691

To get the output sorted and unique from the file you can do something like this:

First reading the file into an array with file(), so you have each line as one array element. Then you can use array_unique() so you only have unique lines/elements.

After that you can sort the array with usort() and apply strcasecmp as callback.

Code:

<?php

    $fileName = "GuestBook.txt";
    $lines = file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $lines = array_unique($lines);

    usort($lines, "strcasecmp");

    print_r($lines);

?>

If you want to store them already unique you can do it like this:

Also read the file into an array with file() and just check with in_array() if you already have the value in your array. If not add the value to the file with file_put_contents().

Code:

<?php

    $name = $_POST['name'];
    $email = $_POST['email'];

    $fileName = "GuestBook.txt";

    $lines = file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

    if(!in_array($name . "----" . $email, $lines)){
        file_put_contents($fileName, $name . "----" . $email . "\r\n", FILE_APPEND);
    }

?>

And if you want to add the value already sorted into the file you can do it like this:

Again get your file into an array, add the value to the array, sort it and you can put it back again.

Code:

<?php

    $name = $_POST['name'];
    $email = $_POST['email'];

    $fileName = "GuestBook.txt";

    $lines = file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $lines[] = $name . "----" . $email;

    usort($lines, "strcasecmp");

    file_put_contents($fileName, implode("\r\n", $lines), FILE_APPEND);


?>

And of course if you want them already unique and sorted just combine the two code snippets from above.

Upvotes: 4

thorne51
thorne51

Reputation: 618

<?php
$fh = fopen('file.txt', 'r');
$lines = array();
while ( $line = fgets($fh) ) {
  $lines[] = $line;
}
$arr = array_unique($lines);
foreach ( $arr as $line ) {
  echo $line . "\r\n";
}

Upvotes: -1

Related Questions