11

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.

enter image description here

asked Jan 22, 2016 at 6:36

3 Answers 3

3
+50

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.

answered Jan 24, 2016 at 6:54
Sign up to request clarification or add additional context in comments.

2 Comments

But when i use Codeigniter "PHPExcel" The output come in Hebrew without changing any settings. So i need such solutions. U know we cant tell all the users to follow the steps to see the data! Is there any other way??
@TomPHP so after a lot of playing around, i believe i have a solution. update answer.
0

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

answered Jan 24, 2016 at 6:56

2 Comments

nope its not working with BOM.. I tried already.. Also i cant insist all users to user Nodepad++ because the default software they will have are ms-excell installed.. So I am looking for a solution to open it in msoffice ..
No need to insist user to use npp. I mentioned it so you can check it once using it. anyway, you found a solution! Way to go!
-1

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.

answered Mar 3, 2024 at 8:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.