本篇文章给大家带来的内容是关于php利用open,fwrite实现导出多种格式的文件(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
效果图
源码分析
<?php /** * Created by PhpStorm. * User: Jason0727 * Date: 2018/8/9 * Time: 14:48 */header("Content-Type: text/html;charset=utf-8");//定义文件名 $fileName = "测试文件名_" . date('YmdHis');//文件名中间不能有空格//文件名转码,以防乱码 $fileName = iconv('UTF-8','GBK',$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 = ['列表1','列表2','列表3']; //初始化写入文件的字符串 $headerTxt = ""; //列表名称转码 foreach ($headerList as $v){ $headerTxt .= $v. $dot; } $headerTxt = rtrim($headerTxt,$dot)."\n"; //将列表名称写入文件句柄 fwrite($fp, $headerTxt); }//初始化数组数据$data = [ ['id'=>'值1','name'=>'值2','hobby'=>'值3'], ['id'=>'值11','name'=>'值22','hobby'=>'值33'], ['id'=>'值111','name'=>'值222','hobby'=>'值333'] ]; //循环写入数据//初始化数组的总数 $count = count($data);//循环次数 $limit = 0;foreach ($data as $v){ $limit++; $txt = $v['id'] .$dot . $v['name'] .$dot .$v['hobby']; if($count != $limit)//避免最后一次换行 $txt .= $dot .PHP_EOL; fwrite($fp,$txt); } fclose($fp);
常见问题分析
字符串中含有特殊的字符,在编码转换时,需要设置//IGNORE,否则会报错
$c = "测试•字符传换•五一快乐!";echo iconv('UTF-8','GBK//IGNORE',$c);//测试字符传换五一快乐!echo iconv('UTF-8','GBK',$c);//Detected an illegal character in input string
数字字符串会出现格式化的情况,需要加如看不见的字符使其正常化,如:”\t”
转码问题,特殊情况,特殊处理,视情况而定
链接:https://pan.baidu.com/s/1SFB3lPooUYl-cVv4QovNkA 密码:2o6b
相关推荐:
PHP如何实现模糊查询(图文代码)以上是php利用open,fwrite实现导出多种格式的文件(代码)的详细内容。更多信息请关注PHP中文网其他相关文章!

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

记事本++7.3.1
好用且免费的代码编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具