php / php/doc-en

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

Open
#1,792 13 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Extension: standard
Dominant language
XML
Stars
596
Forks
890
Avg merge
1d 15h
Merged PRs (30d)
55

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the fgetcsv() behavior with utf8Bom.csv and the supplied PHP script on PHP 8.x, comparing the UTF-8 BOM and SJIS inputs. Check whether the first quoted empty field is returned as [""] or []; if confirmed, determine whether this belongs in the PHP documentation or should be routed to the PHP implementation issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.