标签:utf-8 each tle fclose enc code 数据格式 window下 不可
<?php $arrTitle = array(‘第一列‘, ‘第二列‘, ‘第三列‘, ‘第四列‘, ‘第五列‘, ‘第六列‘); $arrValue[0] = array(‘1‘, ‘2"*x,x‘, "t‘,3", ‘4‘, ‘5‘, ‘6‘); $arrValue[1] = array(‘1‘, ‘2"*x,x‘, "t‘,3", ‘4‘, ‘5‘, ‘6‘); $path = ‘./tttnew2.csv‘; //如果默认编码不是utf8,先用函数utf8_encode将所需写入的数据变成UTF编码格式。 //$msg = utf8_encode($msg); //$msg = iconv(‘gbk‘, ‘utf-8‘, $msg); // 有bom的情况下"\xEF\xBB\xBF"第一次写入这段字符不可缺少 // 为了解决csv数据格式问题,每个格子用双引号那个包含,每个值中的双引号用%22替换 $strBOMHEAD = "\xEF\xBB\xBF"; $fp = @fopen($path, ‘a‘); @fwrite($fp, $strBOMHEAD); //开始写csv @fwrite($fp, implode(‘,‘, $arrTitle) . "\r\n"); foreach ($arrValue as $k => $val) { foreach ($val as $kkey => $vval) { $val[$kkey] = str_replace(‘"‘,‘%22‘,$vval); } $strTmp = ‘"‘.implode(‘","‘, $val).‘"‘; @fwrite($fp, $strTmp. "\r\n"); } @fclose($fp);
生成的csv可以在window下直接打开
标签:utf-8 each tle fclose enc code 数据格式 window下 不可
原文地址:https://www.cnblogs.com/Crius/p/11558664.html