<?php
$row = 1;
$handle = fopen ("test.csv","r");
while ($data = fgetcsv ($handle, 1000, ",")) {
$num = count ($data);
print "<p> $num fields in line $row: <br>\n";
$row++;
for ($c=0; $c < $num; $c++) {
print $data[$c] . "<br>\n";
}
}
fclose ($handle);
?>
上面来自php手册,但是我没有看到在哪里指定编码(如utf8等)
最佳答案
尝试更改区域设置。
就像下面 manual 中的示例所示你给了:
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.
Suggested approach by comment on the same page :
setlocale(LC_ALL, 'ja_JP.UTF8'); // for japanese locale
来自setlocale()
:
Locale names can be found in RFC 1766 and ISO 639. Different systems have different naming schemes for locales. […] On Windows,
setlocale(LC_ALL, '')
sets the locale names from the system's regional/language settings (accessible via Control Panel).
关于php - 在 PHP 中处理 csv 文件时如何指定编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2048347/