duanliaoyu8419 2013-09-05 12:42
浏览 159

如何检测上传的csv文件的编码

I`ve have data.csv file, that must be uploaded to server , parsed ....

This file can have different encodings. I must detect it and convert to utf8.

At this moment php function mb_detect_encoding always return utf8. i tryed:

<?php 
mb_detect_encoding(file_get_contents($_FILES["csv_uploadfile"]["tmp_name"]));

or

<?php 
mb_detect_encoding(file_get_contents($saved_file_path));

mb_detect_encoding returns utf8.

if i use bash command

$ file -bi csv_import_1378376486.csv |awk -F "=" '{print $2}'

it rerurns iso-8859-1

so when i try

iconv --from-code=iso-8859-1 --to-code=utf-8 csv_import_1378382527.csv 

it is not readable.

The real encoding is cp1251, by i cann`t detect it. Can anyone help me to solve this problem?

  • 写回答

1条回答 默认 最新

  • doujing5846 2013-09-12 21:34
    关注

    As someone noticed in the PHP docs here:

    If you try to use mb_detect_encoding() to detect whether a string is valid UTF-8, use the strict mode, it is pretty worthless otherwise.

    So you should try using the true param when detecting encoding:

    mb_detect_encoding($str, mb_detect_order(), TRUE);
    

    If you can predict some possible encodings, you can list them instead of using mb_detect_order().

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度