phpcodeignitercsvhebrew

php write hebrew / UTF-8 in csv


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


Solution

  • 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.