title: PHP - Count number of columns in first line of .csv file
body: <p>I'm trying to count the columns of the first .csv file line.</p>
  
  <p>This is my file content (CRLF -> carriage return and linefeed):
  test.csv</p>
  
  <pre><code>A;B;C;crlf
  </code></pre>
  
  <p>Script:</p>
  
  <pre><code>$handle = fopen($filePath,'r');
  $data = fgetcsv($handle, 1000, ";");
  print_r($data) . PHP_EOL;
  echo 'column nr.: ' . count($data) . PHP_EOL;
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Array
  (
      [0] =&gt; A
      [1] =&gt; B
      [2] =&gt; C
      [3] =&gt; 
  )
  column nr.: 4
  </code></pre>
  
  <p>Why does the function includes the blank column if the line ends after the last <strong>;</strong>
  I expect to count only 3 columns not 4.</p>
  
  <p>Thanks</p>
  
  <p>I'm omiting the last semicolon with the following method:
  Ok,
  the CSV now has 25 columns each line and is created with the following method:</p>
  
  <pre><code>public function createRowForFile(Array $content, $fileName, $sep, $carriageReturnLineFeed) {
      $contentRow = '';
      $noSep = '';
      $lastColumn = count($content);
      $columnNr = 1;
      foreach ($content as $contentName =&gt; $contentValue) {
       // the semicolon after the last column is omited, because semicolonsdefine breaks between columns,
       // (
       // ex: A;B;C;  columns nr = 4 WRONG
       //     A;B;C   columns nr = 3 OK
       // ) 
        if ($lastColumn === $columnNr) {
          $sep = $noSep;
        }
        $columnNr++;
  
        $contentRow.= $contentValue . $sep;
      }
      $contentRow.= $carriageReturnLineFeed;
      $this-&gt;writeFileInDir($fileName, $contentRow);
    }
  </code></pre>
  
  <p>Any suggestion?</p>