Home >Backend Development >PHP Tutorial >PHP uses open and fwrite to export files (code) in multiple formats

PHP uses open and fwrite to export files (code) in multiple formats

不言
不言Original
2018-08-18 17:40:272773browse

The content of this article is about PHP using open and fwrite to export files (code) in various formats. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .

Rendering

PHP uses open and fwrite to export files (code) in multiple formats
PHP uses open and fwrite to export files (code) in multiple formats
PHP uses open and fwrite to export files (code) in multiple formats
PHP uses open and fwrite to export files (code) in multiple formats

#Source code analysis

<?php
/**
 * Created by PhpStorm.
 * User: Jason0727
 * Date: 2018/8/9
 * Time: 14:48
 */header("Content-Type: text/html;charset=utf-8");//定义文件名
 $fileName = "测试文件名_" . date(&#39;YmdHis&#39;);//文件名中间不能有空格//文件名转码,以防乱码
 $fileName = iconv(&#39;UTF-8&#39;,&#39;GBK&#39;,$fileName);///文件后缀
 $ext = ".csv";//$ext = ".txt";//字段间的拼接符号//
 $dot = "|";$dot = ",";//打开文件句柄,记得赋权,否则报错
 $fp = fopen("./files/" . $fileName . $ext,"a") or die("unable to open file!");//是否需要列表名称,0=>不需要 1需要,默认为1
 $is_need_headerList = 1;//检测是否需要
 headerListif($is_need_headerList == 1){    //定义列表名称
    $headerList = [&#39;列表1&#39;,&#39;列表2&#39;,&#39;列表3&#39;];    //初始化写入文件的字符串
    $headerTxt = "";    //列表名称转码
    foreach ($headerList as $v){        
    $headerTxt .= $v. $dot;
    }    
    $headerTxt = rtrim($headerTxt,$dot)."\n";    //将列表名称写入文件句柄
    fwrite($fp, $headerTxt);
}//初始化数组数据$data = [
    [&#39;id&#39;=>&#39;值1&#39;,&#39;name&#39;=>&#39;值2&#39;,&#39;hobby&#39;=>&#39;值3&#39;],
    [&#39;id&#39;=>&#39;值11&#39;,&#39;name&#39;=>&#39;值22&#39;,&#39;hobby&#39;=>&#39;值33&#39;],
    [&#39;id&#39;=>&#39;值111&#39;,&#39;name&#39;=>&#39;值222&#39;,&#39;hobby&#39;=>&#39;值333&#39;]
];
//循环写入数据//初始化数组的总数
$count = count($data);//循环次数
$limit = 0;foreach ($data as $v){    
$limit++;    
$txt = $v[&#39;id&#39;] .$dot . $v[&#39;name&#39;] .$dot .$v[&#39;hobby&#39;];    
if($count != $limit)//避免最后一次换行
        $txt .= $dot .PHP_EOL;
    fwrite($fp,$txt);
}
fclose($fp);

Analysis of common problems

  1. The string contains special characters. During encoding conversion, //IGNORE needs to be set, otherwise an error will be reported

  2. $c = "测试•字符传换•五一快乐!";echo iconv(&#39;UTF-8&#39;,&#39;GBK//IGNORE&#39;,$c);//测试字符传换五一快乐!echo iconv(&#39;UTF-8&#39;,&#39;GBK&#39;,$c);//Detected an illegal character in input string
  1. The numeric string will be formatted, and invisible characters need to be added to normalize it, such as: "\t"

  2. Transcoding problem, Special circumstances, special handling, depending on the situation

Link: https://pan.baidu.com/s/1SFB3lPooUYl-cVv4QovNkA Password: 2o6b

Related recommendations :

php implementation code for exporting Excel files in csv format

How PHP implements fuzzy query (graphic code)

The above is the detailed content of PHP uses open and fwrite to export files (code) in multiple formats. 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