编程语言
首页 > 编程语言> > 在PHP中处理csv文件时如何指定编码?

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

作者:互联网

<?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 07003 and 07004. 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).

标签:fgetcsv,php
来源: https://codeday.me/bug/20191012/1898613.html