title: PHP : Optimizing File Parsing
body: <p>I have a log file of about 200,000 rows . Each row's format is :</p>
  
  <blockquote>
    <p>AAA||BBB|C|DDD||</p>
  </blockquote>
  
  <p>Now I parse the values using the following parsing loop :</p>
  
  <pre><code>$fh = fopen($filename, 'r');
  if($fh === FALSE) {
    return null;
  }
  $result = array();
  while(!feof($fh)) {
    $line = fgets($fh);
    $tokens = explode('||', $line);
    $a = $tokens[0];
    list($b, $c, $d) = explode('|', $tokens[1]);
    // then I can get the values of AAA , BBB , C and DDD and put it into an array
    $result[$a] = array('a' =&gt; $a, 'b' =&gt; $b, 'c' =&gt; $c, 'd' =&gt; $d);
  }
  </code></pre>
  
  <p><code>$result[$a]</code> contains all I need, however the parsing time is ~2.1 second. What can I do to reduce parsing speed ?</p>