从 CSV 文件中提取数据时出现空字段
我有这样的代码:
$row = 1;
if (($handle = fopen("data.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
并且 CSV 文件包含阿拉伯语言的数据,当我在浏览器中打开页面时,我在所有行中的第二个字段中得到空值,但在 CSV 文件中它包含数据。因此,当有阿拉伯语数据时,当显示它时,我会在页面中将其显示为空 - 不是所有阿拉伯语数据,大多数 -
当我放置不包含阿拉伯语数据的文件时,一切正常。
问题是什么?
编辑:
CSV 文件中的行:
我将它们添加为照片,因为复制/过去时不会将数据放入正确的顺序
,我会得到这张照片的输出:
I have this code :
$row = 1;
if (($handle = fopen("data.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
and the CSV file containes data in Arabic language, when I open the page in the browser I get empty values for the second field in all lines but in the CSV file it contains data. so when there is arabic data I get it empty in the page when display it - NOT All arabic data, most of them -
When I put a file that doesn't contain arabic data, every thing is ok.
What is the problem?
EDIT :
lines from CSV file :
I added them as photo because when copy / past will not put data in the right order
and I get in the output this photo :
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题是由于使用不同的语言/字符集创建 CSV 并将其读入 PHP 造成的。
如果字符集不相同,PHP 会尝试进行转换。在这种情况下,转换似乎失败,导致数据丢失。
I believe the problem is due to a different language/character set being used to create the CSV, and read it into PHP.
If the character sets are not the same, PHP tries to make a conversion. In this case, it looks like the conversion is failing such that the data appears to be missing.
我也尝试过不同语言的数据,但是当我使用php代码读取该csv文件时,数据被转换为?????????。在 csv 文件中尝试了很多不同语言的数据,但没有找到成功的结果。所以我得出一个结论,如果我们想要处理涉及英语以外的多种语言的电子表格文件 PHPExcel 将是最好的选择。这是一个很棒的图书馆。它肯定会对你有所帮助。
I have also tried with data of different languages, but when I read that csv file using php code, the data was converted into ????????. Tried a lot with different languages data in csv file but found no successful results. So I came to one conclusion that if we want to deal with spreadsheet file that involve multiple languages other that English PHPExcel would be the best option. It is a wonderful library. It would certainly help you out.