n_yanev n_yanev - 29 days ago 24
PHP Question

PHP fputcsv encoding

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

Answer

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.

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

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