Hi I am trying to write a CSV with HEBREW text in it. It writes some symbol not an Hebrew text. Below is my PHP CODE.
<?php
$list = array (
array('שלטל', 'שלטל', 'שלטל', 'שלטל'),
array('123', '456', '789'),
array('"שלטל"', '"שלטל"')
);
$fp = fopen('file.csv', 'w');
//fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
I checked in internet and added "fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ))" but it didnt work out. Can some one help me.
Below is the out put i am getting.
3 Answers 3
Just ran your code. The text is correctly encoded and generated csv is valid. Opening the CSV in a text editor that supports Hebrew text will show correctly. To open CSV containing Hebrew you need to follow instructions as suggested here
Update:
So turns out MS uses UTF-16 and not only that it uses UTF-16LE (little endian). For MS CSV to open correctly, you need UTF-16LE encoded text, tab delimited file.
$list = array (
array('שלטל', 'שלטל', 'שלטל', 'שלטל'),
array('123', '456', '789'),
array('"שלטל"', '"שלטל"')
);
$fp = fopen('file.csv', 'w');
//UTF-16LE BOM
fputs($fp, chr(0xFF) . chr(0xFE));
foreach ($list as $fields) {
$out = '';
foreach ($fields as $k => $v){
$fields[$k] = mb_convert_encoding($v, 'UTF-16LE', 'UTF-8');
}
// UTF-16LE tab
$out = implode(chr(0x09).chr(0x00), $fields);
// UTF-16LE new line
fputs($fp, $out.chr(0x0A).chr(0x00));
}
fclose($fp);
Above code works, not sure about its efficiency though.
2 Comments
I think I've encountered this before and sometimes some versions of excel just isn't showing proper utf8 characters/fonts. but the file itself have utf8 already.
Try opening your csv into Notepad++ and see if it is a utf8-bom and if its showing utf8 characters
2 Comments
Try this alternative saving as CSV:
$data = "שם פרטי,שם משפחה,עיר\na,b,c\n1,2,3"; // need to use " for \n
file_put_contents("myexcel.csv","\xEF\xBB\xBF"); // BOM with UTF-8 to csv
file_put_contents("myexcel.csv",$data,FILE_APPEND); // add more data
Hope it helps.