php - 在 PHP 中处理 csv 文件时如何指定编码?

标签 php fgetcsv

<?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/

相关文章:

php - 如何使用 PHP 解析 CSV 文件

mysql - 如何从 Mysql_fetch_array 获取单行/数据?

c# - 将表拆分为组 - PHP 和 MYSQL

PHP SQL Server 2008 未返回预期结果

php - 在离线/桌面应用程序中呈现网页内容

php - 在 Laravel 中读取 CSV 文件时出现 UTF-8、二进制数据和特殊字符问题

php - fgetcsv() 不适用于 $_POST 中的变量 $delimiter

php - 我怎样才能在mysql查询中获取当前时间?

php - SQL - 在任意级别加入树时是否有推荐的方法?

php - 在 PHP 中短时间内解析大型 CSV 文件