Prevent character encoding
baltechies opened this issue · 2 comments
baltechies commented
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
nyamsprod commented
Hi @baltechies as described in the documentation you either use
- the
CharsetConverter
class if you have theext-mbstring
installed on your machine. - or the
iconv
stream capabilities if you have theext-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
baltechies commented
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);