2

I have a code like this in a file that is UTF-8 encoded:

<?php
setlocale(LC_ALL, 'cs_CZ.utf8'); //Can be commented out without effect.

$input = "Štěpán,Šafránek";
$result = str_getcsv($input);

echo $result[0]; //output = "těpán";
echo $result[1]; //output = "afránek";
?>

Notice the clipped strings those echoes produce.

Also this works:

<?php
setlocale(LC_ALL, 'cs_CZ.utf8'); //Can be commented out without effect.

$input = "aaaŠtěpán,aaaŠafránek";
$result = str_getcsv($input);

echo $result[0]; //output = "aaaŠtěpán";
echo $result[1]; //output = "aaaŠafránek";
?>

As the input string is part of script there should be no problem with encoding, right? Locale is set correctly, right?

So what is wrong? My resolution is that str_getcsv() is just plain broken. Any alternative way to parse CSV?

It is interesting that on Windows it works fine but on Linux I see this behavior.

Related question here, but resolutions mentioned there did not help: PHP str_getcsv removes umlauts

Community
  • 1
  • 1
Josef Sábl
  • 7,538
  • 9
  • 54
  • 66
  • What encoding is your PHP file in? – Pekka May 20 '13 at 16:06
  • 1
    Possible duplicate: http://stackoverflow.com/questions/12390851/fgetcsv-is-eating-the-first-letter-of-a-string-if-its-an-umlaut – m90 May 20 '13 at 16:09
  • 1
    Setting the correct locale setting like `setlocale(LC_ALL,'de_DE.UTF-8')` fixed it for me (strangely enough it doesn't for you), it is a confirmed & unfixed bug though: https://bugs.php.net/bug.php?id=55507 – m90 May 20 '13 at 16:11
  • Yeah, it is strange. That is why I reposted as the bug description doesn't seem to be accurate enough. Maybe setting locale is forbidden on server? – Josef Sábl May 20 '13 at 16:27
  • Confirmed : setlocale(LC_ALL,'de_DE.UTF-8') or whatever appropriate language, does fix the diacritics problem. – Christian Bonato Jun 21 '16 at 08:05
  • Possible duplicate of [fgetcsv is eating the first letter of a String if it's an Umlaut](http://stackoverflow.com/questions/12390851/fgetcsv-is-eating-the-first-letter-of-a-string-if-its-an-umlaut) – TRiG Jan 10 '17 at 12:59

0 Answers0