0

I'm reading a CSV file, plain and simple:

$handle = fopen( 'example.csv', 'r' );
$lines = [];
while( true )
{
    $data = fgetcsv( $handle );
    if( $data === false )
    {
        break;
    }
    $lines[] = $data;
}
fclose( $handle );

$header = $lines[0];
var_export( $header );
echo "\n";
exit(0);

Everything works fine except that when the first item of the first line is parsed, text delimiter double quotes " (if present) are not stripped away but returned in the parsed data.

Let's see:

CSV File

"ID","Name","Surname"
"a1","John","Doe"
...

Output of the code above

array (
  0 => '"ID"',
  1 => 'Name',
  2 => 'Surname' )

Why "ID" has double quotes around it?


On the second line this doesn't happen:

var_export( $lines[1] );

returns

array (
  0 => 'a1',
  1 => 'John',
  2 => 'Doe' )

Am I missing something?

Paolo
  • 15,233
  • 27
  • 70
  • 91

0 Answers0