thephpleague/csv

Prevent character encoding

baltechies opened this issue · 2 comments

I have data as string and tab delineator. My data has some words like Crème and Jalapeño.
CSV reader and getRecords function converts these values to Cr�me and Jalape�o.

How I can prevent these conversions or encoding?
My input string charset is cp1252.
I am using league/csv version 9.7.3

Hi @baltechies as described in the documentation you either use

  • the CharsetConverter class if you have the ext-mbstring installed on your machine.
  • or the iconv stream capabilities if you have the ext-iconv installed on your machine.
    you cand then do the following:
use League\Csv\CharsetConverter;
use League\Csv\Reader;

$reader = Reader::createFromPath('/path/to/your/csv/file.csv');
CharsetConverter::addTo($writer, 'cp1252.', 'utf-8');

$reader->getRecords(); // should convert you input csv date from 'cp1252.' to 'utf-8'

The code is provided as is without being tested, you may need to adjust it

Thank you @nyamsprod ,
I had implemented that but that was not working. There was an issue in my content. I had to use

$content = iconv("Windows-1252", "UTF-8", $content); 
$csv = \League\Csv\Reader::createFromString($content);