1 Star 0 Fork 0

mmsoft / PHP-CSV

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
CSV.PHP 1.02 KB
一键复制 编辑 原始数据 按行查看 历史
<?php
function fopen_utf8($filename){
$encoding='';
$handle = fopen($filename, 'r');
$bom = fread($handle, 2); //读取文件
// fclose($handle);
rewind($handle); //rewind() 函数将文件指针的位置倒回文件的开头。
// print_r($bom);die();
if($bom === chr(0xff).chr(0xfe) || $bom === chr(0xfe).chr(0xff)){
// UTF16 Byte Order Mark present
$encoding = 'UTF-16';
} else {
$file_sample = fread($handle, 1000) + 'e'; //read first 1000 bytes
// + e is a workaround for mb_string bug
rewind($handle);
$encoding = mb_detect_encoding($file_sample , 'UTF-8, UTF-7, ASCII, EUC-JP,SJIS, eucJP-win, SJIS-win, JIS, ISO-2022-JP');
}
if ($encoding){
stream_filter_append($handle, 'convert.iconv.'.$encoding.'/UTF-8');
}
// print_r($handle);die();
return ($handle);
}
$file='goods.csv';
if (($handle = fopen_utf8($file)) === FALSE) return;
while (($cols = fgetcsv($handle, 1000, "\t")) !== FALSE) {
echo $cols[0];
}
1
https://gitee.com/pjvip/PHP-CSV.git
git@gitee.com:pjvip/PHP-CSV.git
pjvip
PHP-CSV
PHP-CSV
master

搜索帮助