<?php
/**
 * CSV类
 *
 * CSV作类，目前只支持smtp服务的邮件发送
 *
 *
 * @山东破浪网络科技有限公司提供技术支持 授权请购买FeiWa授权
 * @license    http://www.feiwa.org
 * @link       联系电话：0539-889333 客服QQ：2116198029
 */
defined('ByFeiWa') or exit('Access Invalid!');
final class Csv{
	public $filename;
	public function export($data){
		if (!is_array($data)) return;
		$string = '';$new = array();
		foreach ($data as $k=>$v) {
			foreach ($v as $xk=>$xv) {
				$v[$xk] = str_replace(',','',$xv);
			}
			$new[] = implode(',',$v);
		}
		if (!empty($new)){
			$string = implode("\n",$new);
		}
		header("Content-Type: application/vnd.ms-excel; charset=GBK");
		header("Content-Disposition: attachment;filename={$this->filename}.csv");
		echo $string;		
	}
	/**
	 * 转码函数
	 *
	 * @param mixed $content
	 * @param string $from
	 * @param string $to
	 * @return mixed
	 */
    public function charset($content, $from='gbk', $to='utf-8') {
        $from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;
        $to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;
        if (strtoupper($from) === strtoupper($to) || empty($content)) {
            //如果编码相同则不转换
            return $content;
        }
        if (function_exists('mb_convert_encoding')) {
 			if (is_array($content)){
				$content = var_export($content, true);
				$content = mb_convert_encoding($content, $to, $from);
				eval("\$content = $content;");return $content;
			}else {
				return mb_convert_encoding($content, $to, $from);
			}
        } elseif (function_exists('iconv')) {
 			if (is_array($content)){
				$content = var_export($content, true);
				$content = iconv($from, $to, $content);
				eval("\$content = $content;");return $content;
			}else {
				return iconv($from,$to,$content);
			}
        } else {
            return $content;
        }
    }	
}

?>