PHP fputcsv encoding
Asked Answered
R

6

10

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

Reverso answered 19/9, 2012 at 5:15 Comment(0)
L
14

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');
Langmuir answered 19/9, 2012 at 5:21 Comment(5)
When I use this method I have this error: iconv(): Detected an illegal character in input stringReverso
"Default encoding for a csv file is windows1251"...?!?! Who set this default?Copperplate
Just woke up :), windows1252 for EN-USLangmuir
Please change the answer code to reflect Windows-1252 instead of Windows-1251Conjugation
@Conjugation changed :)Langmuir
R
12

It's Working: Enjoy
use this before fputcsv:

$line = array_map("utf8_decode", $line);
Rush answered 12/12, 2014 at 10:10 Comment(4)
underrated comment!Antimonous
That's completely wrong. utf8_decode cannot convert UTF-8 to windows1251. See PHP documentation.Liba
@Liba utf8_decode: Converts a string with ISO-8859-1 characters encoded with UTF-8 to single-byte ISO-8859-1. that resolves the issue... Please check typeerror.org/docs/php/function.utf8-decodeRush
@ArslanTabassum The question was about Windows1251 encoding, not ISO-8859-1. They are different. utf8_decode and utf8_encode functions only work with ISO-8859-1. Using them for anything else is wrong. That is also stated as a note in the documentation. Also this function is deprecated in the upcoming PHP 8.2.0.Liba
C
6

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.

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

Copperplate answered 19/9, 2012 at 5:27 Comment(4)
What if I choose an encoding where the , and " used in the CSV files are encoded differently, like UTF-16?Talkative
Then supply UTF-16 encoded " and , to fputcsv as parameters. (Haven't actually tried it though, but would be my first attempt.)Copperplate
This reply and reading through the articles saved my day. Thank you!Vulcanite
This one is a more usefull link to iconv: php.net/manual/en/function.iconv.phpOptime
T
6
fputs( $fp, $bom = chr(0xEF) . chr(0xBB) . chr(0xBF) );

Try this it worked for me!!!

Tumbleweed answered 8/6, 2017 at 7:0 Comment(1)
Yup, first fputs like above and then fputcsv right after that fixed it for me, too. BOM is the byte order mark which Excel needs to display umlauts and special character correctly, especially on Mac.Mckinley
S
2

Try the iconv function:

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.

Spoor answered 19/9, 2012 at 5:19 Comment(0)
P
1
$string = iconv(mb_detect_encoding($string), 'Windows-1252//TRANSLIT', $string);
Parasang answered 14/2, 2019 at 15:56 Comment(1)
It would help readers if you added further explanation to your answer so they could see the value in your answer compared to the other solution choices. Use edit to improve your answer.Rodomontade

© 2022 - 2024 — McMap. All rights reserved.