csv 中缺少字段的第一个字符
我正在 php 中编写 csv 导入脚本。除了字段开头的外来字符之外,它工作正常。
代码看起来像这样
if (($handle = fopen($filename, "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
$teljing[] = $data;
fclose($handle);
}
这是一个显示我的问题的数据示例
føroyskir stavir, "Kr. 201,50"
óvirkin ting, "Kr. 100,00"
这将导致以下结果
array
(
[0] => array
(
[0] => 'føroyskir stavir',
[1] => 'Kr. 201,50'
)
[1] => array
(
[0] => 'virkin ting', <--- Should be 'óvirkin ting'
[1] => 'Kr. 100,00'
)
)
我已经在 php.net 的一些评论中看到了这种行为,并且我已经尝试过 ini_set('auto_detect_line_endings',TRUE);< /code> 检测行结尾。没有成功。
有人熟悉这个问题吗?
编辑:
谢谢你,AJ,这个问题现在已经解决了。
setlocale(LC_ALL, '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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 PHP 手册 for
fgetcsv()
:“注意:区域设置是如果 LANG 是 en_US.UTF-8,则该函数会错误地读取一字节编码的文件。”
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."
从 PHP.net/fgetcsv 评论复制:
Copied from the PHP.net/fgetcsv comments: