Home >Backend Development >PHP Tutorial >Detailed explanation of examples of CSV format file output in PHP

Detailed explanation of examples of CSV format file output in PHP

零下一度
零下一度Original
2017-07-02 11:15:041686browse

This article mainly introduces the detailed explanation of a few lines of PHP code to achieve CSV format file output. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor to take a look.

Organize the documents, search out a php to implement CSV format file output, and organize and streamline it a little for sharing.


//适用于不需要设置格式简单将数据导出的程序,多多指教......

$str .= 'pro_code'.','.'words'.'\n';//首先写入表格标题栏

foreach($is_error as $key => $value){//循环写入数据

  $str .= $value['pro_code'].",".$value['words']."\n";

}

$str = iconv('utf-8','gb2312',$str);//防止中文乱码

$filename = "./output.csv";//文件路径及名字

export_csv($filename,$str); //导出

 

//自定义输出函数
function export_csv($filename,$str){

  header("Content-type:text/csv");

  header("Content-Disposition:attachment;filename=".$filename);

  header('Cache-Control:must-revalidate,post-check=0,pre-check=0');

  header('Expires:0');

  header('Pragma:public');

  echo $str;

}

The above is the detailed content of Detailed explanation of examples of CSV format file output in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn