Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 609
Chris Avatar asked Jun 06 '11 21:06

Chris


People also ask

How to use fgetcsv in PHP?

PHP fgetcsv() Function$file = fopen("contacts. csv","r"); print_r(fgetcsv($file)); fclose($file);

How do I read a csv file in column wise in PHP?

You can open the file using fopen() as usual, get each line by using fgets() and then simply explode it on each comma like this: <? php $handle = @fopen("/tmp/inputfile. txt", "r"); if ($handle) { while (($buffer = fgets($handle)) !==

How do I count the number of rows in a CSV file in PHP?

CSV rows are separated by line breaks. Therefore, split the rows by line breaks, and you will get an array of rows, which is countable. You're not actually reading from the file pointer. And if you're just counting lines, then count(file("test.


1 Answers

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');
like image 137
Empty Avatar answered Sep 22 '22 17:09

Empty