PHP fputcsv 编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12488954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:39:20  来源:igfitidea点击:

PHP fputcsv encoding

phpcsvencodingexport-to-csv

提问by nyanev

I create csv file with fputcsv. I want csv file to be in windows1251 ecnding. But can't find the solution. How can I do that? Cheers

我用 fputcsv 创建了 csv 文件。我希望 csv 文件在 windows1251 ecnding 中。但是找不到解决办法。我怎样才能做到这一点?干杯

回答by Mihai Iorga

Default encoding for an excel file is machine specific ANSI, mainly windows1252. But since you are creating that file and maybe inserting UTF-8 characters, that file is not going to be handled ok.

excel 文件的默认编码是机器特定的 ANSI,主要是windows1252. 但是由于您正在创建该文件并可能插入 UTF-8 字符,因此该文件将无法正常处理。

You could use iconv()when creating the file. Eg:

您可以iconv()在创建文件时使用。例如:

function encodeCSV(&$value, $key){
    $value = iconv('UTF-8', 'Windows-1252', $value);
}
array_walk($values, 'encodeCSV');

回答by Arslan Tabassum

It's Working: Enjoy
use this before fputcsv:

它正在工作:
在 fputcsv 之前享受使用它:

$line = array_map("utf8_decode", $line);

回答by deceze

The file will be in whatever encoding your strings are in. PHP strings are raw byte arrays, their encodings depends on wherever the bytes came from. If you just read them from your source code files, they're in whatever you saved your source code as. If they're from a database, they're in whatever encoding the database connection was set to.

该文件将采用您的字符串所在的任何编码。PHP 字符串是原始字节数组,它们的编码取决于字节来自何处。如果您只是从源代码文件中读取它们,那么它们就在您将源代码保存为的任何内容中。如果它们来自数据库,则它们采用数据库连接设置的任何编码。

If you need to convert from one encoding to another, use iconv. If you need more in-depth information, see here:

如果您需要从一种编码转换为另一种编码,请使用iconv. 如果您需要更深入的信息,请参阅此处:

回答by pr1nc3

fputs( $fp, $bom = chr(0xEF) . chr(0xBB) . chr(0xBF) );

Try this it worked for me!!!

试试这个它对我有用!!!

回答by Hernan Velasquez

Try the iconv function:

试试 iconv 函数:

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

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

To transform the members of the array you're passing to the fputcsv function to the encoding you want.

要将传递给 fputcsv 函数的数组成员转换为所需的编码。

回答by tfont

$string = iconv(mb_detect_encoding($string), 'Windows-1252//TRANSLIT', $string);