简体   繁体   English

通过php将csv文件转换为csv UTF8

[英]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. 我从客户端获得了.csv文件,但是当我使用PHP读取该文件时,它显示了一些无法读取的文本。 I try to upload and open with google sheet and then export .csv again with google. 我尝试上传并使用Google工作表打开,然后再次使用Google导出.csv。 it work perfect when I read it with php. 当我用php阅读时,它的工作原理非常完美。

Are there any way to convert .csv to .csv UTF8 without using google ? 有什么方法可以在不使用Google的情况下将.csv转换为.csv UTF8? because client will send .csv to server everyday automatic. 因为客户端每天都会自动将.csv发送到服务器。 and then my php script will read and import .csv data into database. 然后我的PHP脚本将读取.csv数据并将其导入数据库。 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 http://destructor.de/charsets/index.htm

My language is Thai so i need to use windows-874. 我的语言是泰语,所以我需要使用Windows-874。 so it should be: 所以应该是:

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

so just using this 2 line 所以只用这2行

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

Thank Sumit :) 感谢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 ). 您可以尝试将“ BOM”添加到CSV文件的开头(即三个字节:EF BB BF- https ://en.wikipedia.org/wiki/Byte_order_mark)。 In my experience CSV files with BOM are interpreted well by Excel. 以我的经验,Excel可以很好地解释带有BOM的CSV文件。 Maybe it will help in your case. 也许它将对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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