fgetcsv()忽略特殊字符,当它们在行的开头! [英] fgetcsv() ignores special characters when they are at the beginning of line!

查看:177
本文介绍了fgetcsv()忽略特殊字符,当它们在行的开头!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的脚本接受一个CSV文件并读取每一行到一个数组。然后我循环通过第一行的每一列(在我的情况下它包含一个调查的问题),我打印出来。调查是法语,每当问题的第一个字符是一个特殊字符(é,ê,ç等)fgetcsv简单地省略它。





我试图调试这个,但我很困惑。我做了一个var_dump文件的内容和字符绝对是:

  var_dump(utf8_encode(file_get_contents($ _ FILES [ 'csv_file'] ['tmp_name']))); 

这里是我的代码:

  if(file_exists($ _ FILES ['csv_file'] ['tmp_name'])&& $ csv = fopen($ _ FILES ['csv_file'] ['tmp_name'],r ))
{
$ csv_arr = array();

//使用CSV文件的所有单元格填充数组
while(!feof($ csv))
{
$ csv_arr [] = fgetcsv $ csv);
}

//关闭文件,不再需要
fclose($ csv);

//这应该遍历第一行的单元格(问题)
foreach($ csv_arr [0] as $ question)
{
echo utf8_encode $ question)。 < br />;
}

}


解决方案

您是否已签出 fgetcsv 上的手动页面?



有这个,例如:

没有任何东西在这里。


注意:此功能会考虑区域设置。如果LANG是例如en_US.UTF-8,该函数读取单字节编码中的文件错误。


此外,看到它总是在行的开头,这可能是真的隐藏断行问题吗?有这样:


注意:如果PHP在Macintosh计算机上读取文件时没有正确识别行尾, auto_detect_line_endings运行时配置选项可能有助于解决此问题。


您可能还想尝试使用不同的行结尾保存文件。 / p>

I have a simple script that accepts a CSV file and reads every row into an array. I then cycle through each column of the first row (in my case it holds the questions of a survey) and I print them out. The survey is in french and whenever the first character of a question is a special character (é,ê,ç, etc) fgetcsv simply omits it.

Special characters in the middle of the value are not affected only when they are the first character.

I tried to debug this but I am baffled. I did a var_dump with the content of the file and the characters are definitely there:

var_dump(utf8_encode(file_get_contents($_FILES['csv_file']['tmp_name'])));

And here's my code:

if(file_exists($_FILES['csv_file']['tmp_name']) && $csv = fopen($_FILES['csv_file']['tmp_name'], "r"))
    {
        $csv_arr = array();

        //Populate an array with all the cells of the CSV file
        while(!feof($csv))
        {
            $csv_arr[] = fgetcsv($csv);
        }

        //Close the file, no longer needed
        fclose($csv);

        // This should cycle through the cells of the first row (questions)
        foreach($csv_arr[0] as $question)
        {
            echo utf8_encode($question) . "<br />";
        }

    }

解决方案

Have you already checked out the manual page on fgetcsv? There is nothing talking about that specific problem offhand, but a number of contributions maybe worth looking through if nothing comes up here.

There's this, for example:

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.

Also, seeing as it's always in the beginning of the line, could it be that this is really a hidden line break problem? There's this:

Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem.

You may also want to try saving the file with different line endings.

这篇关于fgetcsv()忽略特殊字符,当它们在行的开头!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆