缺少csv中字段的第一个字符 [英] Missing first character of fields in csv
问题描述
我正在使用php中的csv导入脚本。它工作正常,除了字段开头的外来字符。
代码如下
if(($ handle = fopen($ filename,r))!== FALSE)
{
while(($ data = fgetcsv($ handle,1000,,))!== FALSE)
$ teljing [] = $ data;
fclose($ handle);
}
这是一个显示我的问题的数据示例
føroyskirstavir,Kr.201,50
óvirkinting,Kr。100,00
这将产生以下
(
[0] => array
(
[0] =>'føroyskirstavir',
[1] =& ,50'
)
[1] => array
(
[0] =>'virkin ting',< --- should be'óvirkinting'
[1] =>'Kr。100,00'
)
)
b $ b我在php.net中的一些注释中看到了这个behaivior,我已经尝试了
ini_set('auto_detect_line_endings',TRUE);
。没有成功。
任何熟悉此问题的人?
编辑:
感谢AJ,这个问题现在解决了。
setlocale(LC_ALL,'en_US.UTF-8');
解决方案>从
fgetcsv()
的 PHP手册:
注意:此功能会考虑区域设置。如果LANG是例如en_US.UTF-8,则此功能会读取单字节编码的文件。
I'm working on a csv import script in php. It works fine, except for foreign characters in the beginning of a field.
The code looks like this
if (($handle = fopen($filename, "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) $teljing[] = $data; fclose($handle); }
Here is a data example showing my issue
føroyskir stavir, "Kr. 201,50" óvirkin ting, "Kr. 100,00"
This will result in the following
array ( [0] => array ( [0] => 'føroyskir stavir', [1] => 'Kr. 201,50' ) [1] => array ( [0] => 'virkin ting', <--- Should be 'óvirkin ting' [1] => 'Kr. 100,00' ) )
I have seen this behaivior documented in some comments in php.net, and I have tried
ini_set('auto_detect_line_endings',TRUE);
to detect line endings. No success.Anyone familiar with this issue?
Edit:
Thanks you AJ, this issue is now solved.
setlocale(LC_ALL, 'en_US.UTF-8');
Was the solution.
解决方案From the PHP manual for
fgetcsv()
:"Note: Locale setting is taken into account by this function. If LANG is e.g. en_US.UTF-8, files in one-byte encoding are read wrong by this function."
这篇关于缺少csv中字段的第一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!