Modify a line with "KEY - AMOUNT" of a file in PHP
<p>this has been bugging me for ages now but i can't figure it out..</p>

<p>Basically i'm using a hit counter which stores unique IP address in a file.  But what i'm trying to do is get it to count how many hits each IP address has made.</p>

<p>So instead of the file reading:</p>

<pre><code>222.111.111.111  
222.111.111.112  
222.111.111.113  
</code></pre>

<p>I want it to read:</p>

<pre><code>222.111.111.111 - 5  
222.111.111.112 - 9  
222.111.111.113 - 41  
</code></pre>

<p>This is the code i'm using:</p>

<pre><code>    $file = "stats.php";  
    $ip_list = file($file);  
    $visitors = count($ip_list);  

    if (!in_array($_SERVER['REMOTE_ADDR'] . "\n", $ip_list))  
    {  
    $fp = fopen($file,"a");  
    fwrite($fp, $_SERVER['REMOTE_ADDR'] . "\n");  
    fclose($fp);  
    $visitors++;  
    }
</code></pre>

<p>What i was trying to do is change it to: </p>

<pre><code>    if (!in_array($_SERVER['REMOTE_ADDR'] . " - [ANY NUMBER] \n", $ip_list))  
    {  
    $fp = fopen($file,"a");  
    fwrite($fp, $_SERVER['REMOTE_ADDR'] . " - 1 \n");  
    fclose($fp);  
    $visitors++;  
    }

    else if (in_array($_SERVER['REMOTE_ADDR'] . " - [ANY NUMBER] \n", $ip_list))  
    {  
    CHANGE [ANY NUMBER] TO [ANY NUMBER]+1
    }
</code></pre>

<p>I think i can figure out the last adding part, but how do i represent the <code>[ANY NUMBER]</code> part so that it finds the IP whatever the following number is?</p>

<p>I realise i'm probably going about this all wrong but if someone could give me a clue i'd really appreciate it.</p>

<p>Thanks.</p>
