Skip to content

fgetcsv() cause wrong parse on specific UTF-8 BOM encoded CSV #1792

Open
@yui-Kitamura

Description

@yui-Kitamura

Description

When the CSV file that its the first value is empty and they are quoted with the double quotation mark,
PHP function fgetcsv parses the double quotation mark as a string value , only when the file was encoded with UTF-8 BOM.

Use the CSV file saved encoding with UTF-8 BOM.

"","key","key2"
"name","val","val2"

*The FIRST CELL is EMPTY

The following code:

<?php
$csvFile = fopen("utf8Bom.csv", "r");
while($line = fgetcsv($csvFile)) {
  for($idx = 0, $size = count($line); $idx < $size; $idx++ ){
    echo "[". $line[$idx] ."]";
  }
  echo "<br>";
}
fclose($csvFile);
?>

Resulted in this output:

[""][key][key2]
[name][val][val2]

The first cell has "" as a value.

But I expected this output instead:

[][key][key2]
[name][val][val2]

NOTE: the CSV file that encoded with SJIS and the same values parses the first "" empty cell as a empty value. It's collect.

Environment: IIS on WinServer2022
Please test on PHP 8.x who can use it..

PHP Version

PHP 7.4.29

Operating System

Windows Server 2022

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions