2

I have a large study conducted with about 50 questions and 70,000 entries, so manually editing or using pivot tables just won't really work, I need to upload the data into a database. I can't get the Japanese characters to be read with any accuracy while using fgcsv(). I've tried setting the locale to UTF-8 and SJIS, but neither one seem to want to read all of the Japanese characters. I read somewhere this might be a bug, but I don't know..

The data looks like this:

Q-004   必須回答    あなたは、以下のどちらにお住まいですか?    S/A
1       北海道 Hokkaido
2       青森県 Aomori
3       岩手県 Iwate
4       宮城県 Miyagi
5       秋田県 Akita

Here is my code:

setlocale(LC_ALL, 'ja_JP.SJIS');
        $fp = fopen($_POST["filename"],'r') or die("can't open file");
            $csv_line = fgetcsv($fp,1024);
            $query = "";
            $count = 0;
            $question = false;
            while($csv_line = fgetcsv($fp,1024)) {

                if (!$question && strpos($csv_line[0],"Q-")!== false)
                {
                    echo "Found a question: ".$csv_line[2] . "<br>";
                    $question = true;
                }
                else if($question && strlen($csv_line[0])==0)
                {
                    echo "<hr>";
                    $question = false;
                }
                else if($question && intval($csv_line[0])>0)
                {
                    echo $csv_line[0]. " has value ". $csv_line[2]." - ".$csv_line[3]. "<br>";
                }       

                $count++;
            }
        echo "$count records read successfully";
        fclose($fp) or die("can't close file");

Here is the result:

Found a question: A以下のどちらにお住まいですか?
1 has value k海道 - Hokkaido
2 has value X県 - Aomori
3 has value - Iwate
4 has value {城県 - Miyagi
5 has value H田県 - Akita
4

1 回答 1

2

当谈到在 PHP 中读取 CSV 时,我会说......不要这样做,而是使用 SQL 数据库,其中您可以在 MySQL中设置诸如ujis_japanese_ci 之类的排序规则。

您应该能够使用 phpMyAdmin 轻松地将您的 CSV 导入 MySQL 数据库(如果您有的话),然后从 MySQL 数据库中呈现数据,而不是读取 CSV 文件。

这是一种解决方法,当然,但我的一般经验是 CSV + 外国/特殊字符 == 问题。

我相信至少值得一试。祝你好运

于 2012-08-15T02:18:56.007 回答