<?php
//获取csv数据
function csvencode($file){
if(!is_file($file['tmp_name'])){
return;
}
$handle=fopen($file['tmp_name'],'r');
$content = fread($handle, filesize($file['tmp_name']));
$content = convert2utf8($content);
$data=explode("\n",$content);
$i=1;
$arr=array();
foreach($data as $o){
if(!ctype_space($o) && !empty($o)){
$arr[$i]=explode(',',$o);
$i++;
}
}
return $arr;
}
//转换为utf8编码
function convert2utf8($content)
{
mb_detect_order("UTF-8, GBK, ISO-8859-1"); //探测顺序
$encoding = mb_detect_encoding($content);//获取编码
if ($encoding && strtolower($encoding) !== 'utf-8') {
$content = iconv($encoding, 'UTF-8//IGNORE', $content);
}
return $content;
}