csv file import encoding error

Solutions on MaxInterview for csv file import encoding error by the best coders in the world

showing results for - "csv file import encoding error"
Lea
12 May 2017
1<?php
2# http://www.practicalweb.co.uk/blog/2008/05/18/reading-a-unicode-excel-file-in-php/
3function fopen_utf8($filename){
4    $encoding='';
5    $handle = fopen($filename, 'r');
6    $bom = fread($handle, 2);
7//  fclose($handle);
8    rewind($handle);
9
10    if($bom === chr(0xff).chr(0xfe)  || $bom === chr(0xfe).chr(0xff)){
11            // UTF16 Byte Order Mark present
12            $encoding = 'UTF-16';
13    } else {
14        $file_sample = fread($handle, 1000) + 'e'; //read first 1000 bytes
15        // + e is a workaround for mb_string bug
16        rewind($handle);
17
18        $encoding = mb_detect_encoding($file_sample , 'UTF-8, UTF-7, ASCII, EUC-JP,SJIS, eucJP-win, SJIS-win, JIS, ISO-2022-JP');
19    }
20    if ($encoding){
21        stream_filter_append($handle, 'convert.iconv.'.$encoding.'/UTF-8');
22    }
23    return  ($handle);
24} 
25?>