title: Extracting data from log file with PHP
body: <p>I have a server log file from which I am trying to create a PHP page which summaries the data it stores. Each record in the log is stored on a new line, and in the format:</p>
  
  <pre><code>207.3.35.52 -- [2007-04-01 01:24:42] "GET index.php HTTP/1.0" 200 11411 "Mozilla/4.0"
  
  //ip -- [timestamp] "GET url HTTP/1.0" status code bytes "user agent".
  </code></pre>
  
  <p>I am trying to write a summary which displays: the total amount of requests, the total amount of requests form the articles directory, the total bandwidth consumed and finally the amount of 404 errors and their pages.</p>
  
  <p>PHP:</p>
  
  <pre><code>$handle = fopen('logfiles/april.log','r') or die ('File opening failed');
  $requestsCount = 0;
      while (!feof($handle)) {
          $dd = fgets($handle);
          $requestsCount++;   
          $parts = explode('"', $dd);
          $statusCode = substr($parts[2], 0, 4);
          }
  fclose($handle);
  </code></pre>
  
  <p>This code opens the file and counts the amount of records, seperates and finds the status code number in the record. When echoing $statusCode it does show the correct information, showing all the status codes in the log.</p>
  
  <p>A function which accepts two arguements to total the 404 errors:</p>
  
  <pre><code>function requests404($l,$s) {
          $r =  substr_count($l,$s);
          return "Total 404 errors: ".$r."&lt;br /&gt;";
  }
  </code></pre>
  
  <p>Echo the result: </p>
  
  <pre><code>echo requests404($statusCode, '404');
  </code></pre>
  
  <p>This function doesn't work, it just returns 0. Working with txt files in PHP is my weakest skill and I would really appreciate some help as I think I may be going about it the complete wrong way. Thanks.</p>