我在一个UTF-8编码的文件中有这样的代码:
<?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";
?>
注意那些回声产生的剪切字符串。
这也有效:
<?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";
?>
由于输入字符串是脚本的一部分,编码应该没有问题,对吧?区域设置设置正确,对吗?
那有什么不对?我的解决方案是str_getcsv()只是简单的破解。有没有其他解析CSV的方法?
有趣的是,在Windows上它工作正常但在Linux上我看到了这种行为。
这里有相关问题,但那里提到的决议没有帮助: PHP str_getcsv removes umlauts