1

I'm trying to count the columns of the first .csv file line.

This is my file content (CRLF -> carriage return and linefeed): test.csv

A;B;C;crlf

Script:

$handle = fopen($filePath,'r');
$data = fgetcsv($handle, 1000, ";");
print_r($data) . PHP_EOL;
echo 'column nr.: ' . count($data) . PHP_EOL;

Output:

Array
(
    [0] => A
    [1] => B
    [2] => C
    [3] => 
)
column nr.: 4

Why does the function includes the blank column if the line ends after the last ; I expect to count only 3 columns not 4.

Thanks

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:

public function createRowForFile(Array $content, $fileName, $sep, $carriageReturnLineFeed) {
    $contentRow = '';
    $noSep = '';
    $lastColumn = count($content);
    $columnNr = 1;
    foreach ($content as $contentName => $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->writeFileInDir($fileName, $contentRow);
  }

Any suggestion?

4
  • This is happening because there is an extra ; at the end of the line. The line endings are not actually the issue. Your line should be A;B;C\r\n. Commented Oct 1, 2015 at 16:24
  • No, PHP runs on IBMI CSV is created on Windows, The end of file is different, but how can I solve this issue? Commented Oct 1, 2015 at 16:27
  • 1
    @ Rocket Hazmat this is the output now: Array ( [0] => A [1] => B [2] => C\r\n ) colum nr.: 3 I don't want the \r\n in my column data Commented Oct 1, 2015 at 16:31
  • If you are creating the CSV file with PHP, then I'd suggest using fputcsv(). Commented Oct 2, 2015 at 17:04

2 Answers 2

1

You have 3 semicolons in your file, which means it defines 4 columns. And, if you execute this on Linux, please check http://php.net/manual/en/filesystem.configuration.php#ini.auto-detect-line-endings to make fgetcsv detect the line endings properly.

Sign up to request clarification or add additional context in comments.

Comments

1

Traditionally the last column does not have the delimiter ; after it. PHP is assuming you have 4 columns and not 3. Remove the last semi-colon to get a result of 3.

2 Comments

Yes, i've alredy experimented this solution. But this behaviour seems unintuitive to me. At least from my point of view, the semicolons indicates the end of the column, not the begining. But maybe this was only what I was hoping in order to make more simple the script. Without having to omit the semicolon for the last column. Thanks to all
The semicolons define breaks between columns, not the end of a column. Traditionally newlines (but it could be another character) define when a row has ended. How was your CSV data created? Can you control the ending semicolon? If the dataset is of a reasonable size you can always remove the last column before working with the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.