Ryo
Ryo

Reputation: 131

convert csv file to csv UTF8 by php

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

Upvotes: 1

Views: 1882

Answers (2)

Ryo
Ryo

Reputation: 131

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 :)

Upvotes: 1

krzy-wa
krzy-wa

Reputation: 471

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.

Upvotes: 0

Related Questions