Sort and remove duplicates line by line in text file
<p>So I have this text file that I wanted to sort. The contents of the text file looks something like this:
<strong><em>I used "\r\n" at the end of each line when I was writing them in</em></strong></p>

<p>Gere is the code that I used writing this:</p>

<pre><code>$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);
</code></pre>

<p>Opening txt file on the notepad it looks something like this:</p>

<pre><code>aaa----aaa
ggg----ggg
sss----sss
www----www
ttt----ttt
ppp----ppp
ggg----ggg
zzz----zzz
www----www
</code></pre>

<p>Now I want it to display it sorted with duplicates removed:</p>

<pre><code>aaa----aaa
ggg----ggg
ppp----ppp
sss----sss
ttt----ttt
www----www
zzz----zzz
</code></pre>

<p>This is what I did for sorting them:</p>

<pre><code>$fileName = "GuestBook.txt";

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

sort($split);

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

file_put_contents($fileName, $data);
</code></pre>

<p>This is what I have for removing duplicates</p>

<pre><code>$fileName = "GuestBook.txt";

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

file_put_contents($fileName, implode("\n",$lines));
</code></pre>