title: PHP remove all newlines and spaces from a csv
body: <p>I am trying to convert a CSV to JSON using this awesome Jist: <a href="https://gist.github.com/robflaherty/1185299" rel="nofollow">https://gist.github.com/robflaherty/1185299</a></p>
  
  <p>I have been given a very CSV from a very antiquated procedural programming language. The CSV can ONLY be produced as follows.</p>
  
  <p>I need to strip out the second line which is a blank newline and ideally, I'd like to strip out all the extra spaces for each line:</p>
  
  <pre><code>Series,           SKU,                           Stock
  
   01000      ,     01000-1116             ,          98
   01000      ,     01000-1132             ,           0
   01000      ,     01000-116              ,        1000
   01000      ,     01000-1164             ,        3880
   01000      ,     01000-12               ,        2040
   01000      ,     01000-132              ,        2240
   01000      ,     01000-1332             ,         545
   01000      ,     01000-1364             ,          50
  </code></pre>
  
  <p>I've tried every combination in my small arsenal to get this to work. preg_replace, trim etc.</p>
  
  <p>Has anyone got any advice? Here is the section of the Jist that is handling the rows:</p>
  
  <pre><code>function csvToArray($file, $delimiter) {
      if (($handle = fopen($file, 'r')) !== FALSE) {
  
          $i = 0;
          while (($lineArray = fgetcsv($handle, 4000, $delimiter, '"')) !== FALSE) {
              for ($j = 0; $j &lt; count($lineArray); $j++) {
                  $arr[$i][$j] = $lineArray[$j];
              }
              $i++;
          }
          fclose($handle);
      }
      return $arr;
  }
  </code></pre>
  
  <p>I hope someone can help! Thanks in advance for taking a look.</p>