Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Bounty Awarded with 50 reputation awarded by Community Bot
added working solution
Source Link
mani
  • 3.1k
  • 2
  • 17
  • 25

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.

header ("Content-type: text/csv; charset=UTF-16LE");
header('Content-Disposition: attachment; filename="filename.csv";');
$list = array (
 array('שלטל', 'שלטל', 'שלטל', 'שלטל'),
 array('123', '456', '789'),
 array('"שלטל"', '"שלטל"')
 );
$fp = fopen('php://stdout''file.csv', 'w');
//UTF-16LE BOM
echofputs($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
 echofputs($fp, $out.chr(0x0A).chr(0x00));
}
fclose($fp);

Above code works, not sure about its efficiency though.

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.

header ("Content-type: text/csv; charset=UTF-16LE");
header('Content-Disposition: attachment; filename="filename.csv";');
$list = array (
 array('שלטל', 'שלטל', 'שלטל', 'שלטל'),
 array('123', '456', '789'),
 array('"שלטל"', '"שלטל"')
 );
$fp = fopen('php://stdout', 'w');
//UTF-16LE BOM
echo 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
 echo $out.chr(0x0A).chr(0x00);
}
fclose($fp);

Above code works, not sure about its efficiency though.

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.

added working solution
Source Link
mani
  • 3.1k
  • 2
  • 17
  • 25

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.

header ("Content-type: text/csv; charset=UTF-16LE");
header('Content-Disposition: attachment; filename="filename.csv";');
$list = array (
 array('שלטל', 'שלטל', 'שלטל', 'שלטל'),
 array('123', '456', '789'),
 array('"שלטל"', '"שלטל"')
 );
$fp = fopen('php://stdout', 'w');
//UTF-16LE BOM
echo 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
 echo $out.chr(0x0A).chr(0x00);
}
fclose($fp);

Above code works, not sure about its efficiency though.

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

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.

header ("Content-type: text/csv; charset=UTF-16LE");
header('Content-Disposition: attachment; filename="filename.csv";');
$list = array (
 array('שלטל', 'שלטל', 'שלטל', 'שלטל'),
 array('123', '456', '789'),
 array('"שלטל"', '"שלטל"')
 );
$fp = fopen('php://stdout', 'w');
//UTF-16LE BOM
echo 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
 echo $out.chr(0x0A).chr(0x00);
}
fclose($fp);

Above code works, not sure about its efficiency though.

Source Link
mani
  • 3.1k
  • 2
  • 17
  • 25

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

lang-php

AltStyle によって変換されたページ (->オリジナル) /