NYCPHP Meetup

[nycphp-talk] fgetcsv and German characters

csnyder chsnyder at gmail.com
Tue Aug 14 11:20:12 EDT 2007


On 8/14/07, Stephen Musgrave <stephen at capellic.com> wrote:
> Oh, how I love character encoding problems!  ;-)
>
> I have a file upload utility whereby the user uploads a CSV document.  The
> file is saved to a temporary folder.  I open that document, use fgetcsv() to
> pull the values and then insert them into a database.
>
> The problem is that German characters such as ü and ä are being translated
> to Ÿ and Š respectively when I echo the values to the browser and when
> inserted into the DB.  I wrote the values to the error log and they come out
> to \x9f and \x8a respectively.
>
> I learned that I should use the setlocale() function for German:
>
>  $loc_de = setlocale(LC_ALL, 'de_DE');
>
> But that didn't do anything for me.  I even tried as shown in the example
> for setlocale() on php.net:
>
>  $loc_de = setlocale(LC_ALL, 'de_DE at euro', 'de_DE', 'de', 'ge');
> When I echo $loc_de, both say "de_DE" as expected, but it doesn't fix my
> import problem.
>
> I've checked the uploaded file contents and it looks fine.
>
> Any suggestions would be appreciated.
>
> Thanks,
>
> Stephen


Locale wouldn't matter in this case; that's primarily useful when
generating content (such as formatted dates or gettext translations).

You need to figure out what encoding was used on the original csv
document, and then use iconv functions or similar to convert it to
utf-8 or whatever encoding is being used by your system for storage
and display.

How to do that? I would open the csv file in a web browser and play
with the character encodings until it looked right--View>Character
Encoding in Firefox--but there might be a more systematic way...

-- 
Chris Snyder
http://chxo.com/


More information about the talk mailing list