How to get the amount each line appears from a file?
<p>I'm trying to read a txt file and count how many times each line appears in the file.</p>

<p>heroes.txt:</p>

<pre><code>Hero1, Hero2, Hero3, Hero4, Hero5, Hero6, Hero7, Hero8
Hero1, Hero8, Hero9, Hero10, Hero11, Hero12, Hero13, Hero14
Hero1, Hero2, Hero3, Hero4, Hero5, Hero6, Hero7, Hero8
Hero2, Hero3, Hero4, Hero5, Hero6, Hero7, Hero8, Hero9
Hero1, Hero2, Hero3, Hero4, Hero5, Hero6, Hero7, Hero8
Hero1, Hero8, Hero9, Hero10, Hero11, Hero12, Hero13, Hero14
</code></pre>

<p>Expected output:</p>

<pre><code>Appeared 3x: Hero1, Hero2, Hero3, Hero4, Hero5, Hero6, Hero7, Hero8
Appeared 2x: Hero1, Hero8, Hero9, Hero10, Hero11, Hero12, Hero13, Hero14
Appeared 1x: Hero2, Hero3, Hero4, Hero5, Hero6, Hero7, Hero8, Hero9
</code></pre>

<p>So far I can do this for 1 line which I save in a variable and use it to compare it to all other lines. Right now I go through each line and check with <code>strpos()</code> if the saved line from the variable is the same as the one I read currently in the while loop. If yes I increment my counter variable.</p>

<p>But as you can see with this code it only works for 1 line, which I also have to save first to a variable. </p>

<p>So how can I modify my code, that it counts how many times each line appears in the file?</p>

<p>Code:</p>

<pre><code>&lt;?php

    $herocombo = "Hero1, Hero2, Hero3, Hero4, Hero5, Hero6, Hero7, Hero8";

    $handle = fopen ("heroes.txt", "r");
    $counter = 0;

    while (!feof($handle))
    {
        $line = fgets($handle);
        $pos = strpos($line, $herocombo);

        if ($pos !== false)
        {
           $counter++;
        }
    }

    fclose ($handle);

    echo "Popular Combo:";
    echo "\n";
    echo "Appreared" . $counter ."times:" . $herocombo;
    echo "\n";

?&gt;
</code></pre>