Skip to content

CSV file with UTF-16LE encoding iconv() error #524

@kaoz70

Description

@kaoz70

This is:

- [ X] a bug report
- [ ] a feature request)

What is the expected behavior?

Correctly load a CSV file with UTF-16LE encoding.

What is the current behavior?

When loading this file, iconv() throws the error:

iconv(): Detected an incomplete multibyte character in input string

This happens in the method:

\PhpOffice\PhpSpreadsheet\Shared\StringHelper::convertEncoding()

Changing the line from:

$result = iconv($from, $to . '//IGNORE//TRANSLIT', $value);

To:

$result = iconv(mb_detect_encoding($from, mb_detect_order(), false), $to . '//IGNORE//TRANSLIT', $value);

Fixes the issue, but I'm not sure if there are any side effects by doing this.

What are the steps to reproduce?

<?php

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

$path = 'test.csv';

// Create new Spreadsheet object
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
$reader->setInputEncoding('UTF-16LE');
$reader->setDelimiter('	'); // Tab delimiter
$spreadsheet = $reader->load($path));

### Which versions of PhpSpreadsheet and PHP are affected?
PhpSpreadsheet 1.2.1
PHP 7.2

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