Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

empty cells don't have "", #221

Open
vitali-bc opened this issue Nov 15, 2024 · 2 comments
Open

empty cells don't have "", #221

vitali-bc opened this issue Nov 15, 2024 · 2 comments

Comments

@vitali-bc
Copy link

basically if a empty cell has "", it will be converter in , only... and it's wrong...
my code (but doesn't work)


    $csv = new \ParseCsv\Csv();  // Correct instantiation
    $csv->delimiter = ',';
    $csv->enclose_all = true;
    $csv->parseFile($filePath);
    
     foreach ($csv->data as &$row) {

        // Normalize empty cells to `""`
        foreach ($row as &$value) { 
            // Only change null or other empty-like values (excluding already empty strings) to ""
            if ($value === null || trim($value) === '') {  
                $value = ''; 
            }
        }
    }
@gogowitsch
Copy link
Member

gogowitsch commented Nov 19, 2024

Could you attach the same file once as a correct CSV and once as a correct spreadsheet, for example a .ods, .xls or .xlsx file? By correct I mean the exact way you expect it.

That way, I can better understand what you expect.

If you want to preserve "" in cells, first try @jimeh’s suggestion below. If that doesn’t work at all and you control both producer and consumer of the files, maybe it helps to change the enclosing char using $csv->enclosure = "!";, assuming your file does not contain any literal ! characters. See https://github.com/parsecsv/parsecsv-for-php/blob/main/src/Csv.php#L106

@jimeh
Copy link
Member

jimeh commented Nov 20, 2024

If you want two literal double quote characters as the value of a field, the correct way to encode them as CSV is to quote the field, and escape the double quotes, by making each one two double quotes:

id,name
1001,""""""
1002,"Johnny ""Two-Toes"" Smith"

The above would be parsed as:

[
  [
    'id' => '1001',
    'name' => '""'
  ],
  [
    'id' => '1002',
    'name' => 'Johnny "Two-Toes" Smith'
  ]
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants