简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM