parsecsv / parsecsv-for-php

CSV data parser for PHP.
MIT License
681 stars 176 forks source link

detect encoding How To? #187

Closed osc2nuke closed 4 years ago

osc2nuke commented 4 years ago

I try to figure out how to use : $csv->encoding('UTF-16', 'UTF-8');

My csv files vary from ISO-8859-1 till US-ASII.

The doc say i must load before parsing: $csv->encoding('UTF-16', 'UTF-8');

But i must know before no? so i can do something like:

$detectedEncoding = $csv->detect_encoding(); $csv->encoding($detectedEncoding , 'UTF-8');//and ignore if is already UTF-8

Does anyone know? Thanks in advanced.

gogowitsch commented 4 years ago

Detecting the encoding is outside the scope of this library.

You can use functions such as mb_detect_encoding().

Here is an example of how to use it:

$filename = 'examples/people.csv';

$csv = new ParseCsv\Csv();
$csv->file_data = file_get_contents($filename);
$deteced_encoding = mb_detect_encoding($csv->file_data);
$csv->encoding($deteced_encoding);
$csv->auto();

I'll close this issue now. If you have any further questions, feel free to re-open and ask.