开发者

How do I get fgetcsv() in PHP to work with Japanese characters?

I have the following data being generated from a google spreadsheet rss feed.

いきます,go,5 
きます,come,5 
かえります,"go home, return",5 
がっこう,school,5 
スーパー,supermarket,5 
えき,station,5 
ひこうき,airplane,5 

Using PHP I can do the following:

$url = 'http://google.com.....etc/etc';
$data = file_get_contents($url);

echo $data; // This prints all Japanese symbols

But if I use:

$url = 'http://google.com.....etc/etc';
$handle = fopen($url);

while($row = fgetcsv($handle)) {
    print_r($row); // Outputs [0]=>,[1]=>'go',[2]=>'5', etc, i.e. the Japanese characters are skipped
}

So it appears the Japanese characters are skipped when using either fopen or fgetcsv.

My file is saved 开发者_如何转开发as UTF-8, it has the PHP header to set it as UTF-8, and there is a meta tag in the HTML head to mark it as UTF-8. I don't think it's the document it's self because it can display characters through the file_get_contents method.

Thanks


I can't add comment to the answer from Darien

I reproduce the problem, after change a locale the problem was solved. You must install jp locale on server before trying repeat this.

Ubuntu Add a new row to the file /var/lib/locales/supported.d/local

ja_JP.UTF-8 UTF-8

And run command

sudo dpkg-reconfigure locales

Or

sudo locale-gen

Debian Just execute "dpkg-reconfigure locales" and select necesary locales (ja_JP.UTF-8)

I don't know how do it for other systems, try searching by the keywords "locale-gen locale" for your server OS.

In the php file, before open csv file, add this line

setlocale(LC_ALL, 'ja_JP.UTF-8');


This looks like it might be the same as PHP Bug 48507.

Have you tried changing your PHP locale setting prior to running the code and resetting it afterwards?


You might want to consider this library. I remember using it some time back, and it is much nicer than the built-in PHP functions for handling CSV files. がんばって!


May be iconv character encoding help you

http://php.net/manual/en/function.iconv.php


You can do that by hand not using fgetcsv and friends:

<?php
$file = file('http://google.com.....etc/etc');
foreach ($file as $row) {
    $row = preg_split('/,(?!(?:[^",]|[^"],[^"])+")/', trim($row));
    foreach ($row as $n => $cell) {
        $cell = str_replace('\\"', '"', trim($cell, '"'));
        echo "$n > $cell\n";
    }
}

Alternatively you can opt in for a more fancy closures-savvy way:

<?php
$file = file('http://google.com.....etc/etc');

array_walk($file, function (&$row) {
    $row = preg_split('/,(?!(?:[^",]|[^"],[^"])+")/', trim($row));
    array_walk($row, function (&$cell) {
        $cell = str_replace('\\"', '"', trim($cell, '"'));
    });
});

foreach ($file as $row) foreach ($row as $n => $cell) {
    echo "$n > $cell\n";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜