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() formats a line (passed as a fields array) as CSV and writes it (terminated by a newline) to the specified file stream .
To convert an array into a CSV file we can use fputcsv() function. The fputcsv() function is used to format a line as CSV (comma separated values) file and writes it to an open file.
Append a new row to the existing CSV file using writerOpen your existing CSV file in append mode Create a file object for this file. Pass this file object to csv.
It's Working: Enjoy
use this before fputcsv:
$line = array_map("utf8_decode", $line);
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With