Skip to content

[suggestion] detect character encoding for CSVs #1647

@andy-educake

Description

@andy-educake

This is:

- [ ] a bug report
- [*] a feature request
- [*] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

When loading a CSV file, it assumes UTF-8. However if you don't know the encoding, you need to open the file to try and detect what encoding is used. It would be good to have a "detect" option as the openFileOrMemory() method on the CSV reader already opens the file and converts to UTF-8.

What is the current behavior?

Either the reader assumes the data is UTF-8 (in which case fgetcsv can corrupt the data if it's not UTF-8) or you need to open the file manually beforehand to supply the correct encoding.

Possible API to provide this functionality

<?php

require __DIR__ . '/vendor/autoload.php';

// Create new Spreadsheet object
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
$reader->setInputEncoding('detect');
//or 
$reader->setAutoDetectInputEncoding(true);

$spreadsheet = $reader->load($pathToFile);

Which versions of PhpSpreadsheet and PHP are affected?

We are using 1.14.1 and that is definitely affected.

I'm happy to have a go at creating a PR if you think this is a valuable addition?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions