1

I got .csv file from client but when I read it with PHP, it show some text can't read. I try to upload and open with google sheet and then export .csv again with google. it work perfect when I read it with php.

Are there any way to convert .csv to .csv UTF8 without using google ? because client will send .csv to server everyday automatic. and then my php script will read and import .csv data into database. please help

Thanks

Ryo
  • 131
  • 1
  • 14
  • I already try with header('Content-Type: text/html; charset=utf-8'); and iconv_set_encoding("internal_encoding", "UTF-8"); , both are doesn't work. – Ryo Apr 25 '15 at 13:02
  • Check this answer http://stackoverflow.com/questions/13298353/php-fgetcsv-charset-encoding-problems – Sumit Apr 25 '15 at 13:02
  • it doesn't work I try to use echo iconv( "Windows-1252", "UTF-8", $data[$c] ) follow their code. it still show unreadable text. Please help – Ryo Apr 25 '15 at 14:29

2 Answers2

1

I found the way, first using:

header('Content-Type: text/html; charset=utf-8');

and we need to find which encode language we use from here

http://destructor.de/charsets/index.htm

My language is Thai so i need to use windows-874. so it should be:

iconv("windows-874", "UTF-8", $data[$c]);

so just using this 2 line

header('Content-Type: text/html; charset=utf-8');
iconv("windows-874", "UTF-8", $data[$c]);

Thank Sumit :)

wnull
  • 217
  • 6
  • 21
Ryo
  • 131
  • 1
  • 14
0

You can try to add "BOM" to the beginning of CSV file (ie. three bytes: EF BB BF - https://en.wikipedia.org/wiki/Byte_order_mark). In my experience CSV files with BOM are interpreted well by Excel. Maybe it will help in your case.

krzy-wa
  • 441
  • 1
  • 4
  • 5