php使用fputcsv()函数csv文件读写数据的方法


Posted in PHP onJanuary 06, 2015

本文实例讲述了php使用fputcsv()函数csv文件读写数据的方法。分享给大家供大家参考。具体分析如下:

fputcsv() 函数用于将数据格式为csv格式,以便写入文件或者数据库.

1.将字符串写入csv文件中,代码如下:

$test_array = array( 

    array("111","sdfsd","sdds","43344","rrrr"), 

    array("sssssssss","gdfgfd","232323","wwewe","dsfds"), 

    array("fgfg","e4343","dsfds","w2332","xcvxc"), 

    array("11212","2323","344343","344343","rerreer"), 

    array("fds","43344444","33333333","ttttttt","gggggggggggg"), 

    array("kdfs","dsfdsfds","wewewe","sdsdddddddd","wwwwwwwwwww")

); 

 

$file = fopen("test.csv","w") or die("Can't Open test.csv"); 

foreach($test_array as $line_array) 

{ 

    $isSuccess = fputcsv($file,$line_array); 

    print $isSuccess."<br>"; 

 if($isSuccess===false) 

    { 

        die("Can't write csv line".$line_array); 

    } 

} 

fclose($file) or die("Can't close file test.csv.");

fputcsv()函数返回所写入行的字符的个数或者false,当写入失败时返回false.

2.将格式化的csv字符串保存到字符串中,代码如下:

$test_array = array( 

        array("111","sdfsd","sdds","43344","rrrr"), 

        array("sssssssss","gdfgfd","232323","wwewe","dsfds"), 

        array("fgfg","e4343","dsfds","w2332","xcvxc"), 

        array("11212","2323","344343","344343","rerreer"), 

        array("fds","43344444","33333333","ttttttt","gggggggggggg"), 

        array("kdfs","dsfdsfds","wewewe","sdsdddddddd","wwwwwwwwwww") 

); 

ob_start(); 

$file = fopen("php://output","w") or die("Can't Open php://output"); 

foreach($test_array as $line_array) 

{ 

        $isSuccess = fputcsv($file,$line_array); 

        if($isSuccess===false) 

        { 

            die("Can't write csv line".$line_array); 

        } 

}
fclose($file) or die("Can't close file test.csv."); 

$result = ob_get_contents(); 

ob_end_clean();

以用fgetcsv(file,length,separator,enclosure)函数读取csv文件.

fgetcsv的参数说明如下:

file:需要读取的csv文件,此参数是必需的。

length:表示大于csv文件中最长的行的长度的值。php5之前是必需参数。在php5中是可选参数,如果不设置此参数或者将其设为0,php将会读取.

一整行的数据。如果行的长度超过8192个字节时,应该将length值设定一个数,而不是让php自动去计算行的长度。

separator:指定数据的分隔符,默认是逗号,如果指定为“;”,那么fgetcsv函数将按照“;”来解析行数据。

fgetcsv的返回值:

根据file的一行数据,返回一个数组,如果读取文件出错,则返回false,到达文件尾部时,也返回false.

下面是一个读取test.csv文件的例子:

$file = fopen('test.csv','r') or die("Can't open file test.csv"); 

$color="#ff0000"; 

print '<table border=0>'; 

while($csv_line=fgetcsv($file)) 

{ 

        print "<tr>"; 

        $len = count($csv_line); 

        for($i=0;$i<$len;$i++) 

        { 

            if($i%2==0)$color="#cccccc"; 

            else $color="#999999"; 

            print '<td bgcolor='.$color.'>'.htmlentities($csv_line[$i]).'</td>'; 

        } 

        print "</tr>"; 

} 

print '</table>'; 

fclose($file) or die("Can't close file test.csv!");

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
PHP 如何向 MySQL 发送数据
Oct 09 PHP
php 字符串函数收集
Mar 29 PHP
PHP学习之整理字符串
Apr 17 PHP
基于php socket(fsockopen)的应用实例分析
Jun 02 PHP
国外十大最流行的PHP框架排名
Jul 04 PHP
Linux下手动编译安装PHP扩展的例子分享
Jul 15 PHP
php实现的click captcha点击验证码类实例
Sep 23 PHP
ThinkPHP处理Ajax返回的方法
Nov 22 PHP
基于PHP实现短信验证码接口(容联运通讯)
Sep 06 PHP
PHP利用二叉堆实现TopK-算法的方法详解
Apr 24 PHP
PHP实现打包zip并下载功能
Jun 12 PHP
Windows平台PHP+IECapt实现网页批量截图并创建缩略图功能详解
Aug 02 PHP
phplot生成图片类用法详解
Jan 06 #PHP
写一段简单的PHP建立文件夹代码
Jan 06 #PHP
php读取flash文件高宽帧数背景颜色的方法
Jan 06 #PHP
php自动获取关键字的方法
Jan 06 #PHP
windows7下php开发环境搭建图文教程
Jan 06 #PHP
PHP中$this和$that指针使用实例
Jan 06 #PHP
php缓冲输出实例分析
Jan 05 #PHP
You might like
php ios推送(代码)
2013/07/01 PHP
php json转换成数组形式代码分享
2014/11/10 PHP
php提交表单发送邮件的方法
2015/03/20 PHP
PHP闭包函数传参及使用外部变量的方法
2016/03/15 PHP
php7基于递归实现删除空文件夹的方法示例
2017/06/15 PHP
yii2学习教程之5种内置行为类详解
2017/08/03 PHP
在laravel框架中使用model层的方法
2019/10/08 PHP
在js中使用&quot;with&quot;语句中跨frame的变量引用问题
2007/03/08 Javascript
Jquery.LazyLoad.js修正版下载,实现图片延迟加载插件
2011/03/12 Javascript
jQuery之过滤元素操作小结
2013/11/30 Javascript
js 获取、清空input type=&quot;file&quot;的值示例代码
2014/02/19 Javascript
22点关于jquery性能优化的建议
2014/05/28 Javascript
jQuery实现防止提交按钮被双击的方法
2015/03/24 Javascript
JavaScript使表单中的内容显示在屏幕上的方法
2015/06/29 Javascript
IE8兼容Jquery.validate.js的问题
2016/12/01 Javascript
Bootstrap基本组件学习笔记之input输入框组(9)
2016/12/07 Javascript
基于Vue2.0+ElementUI实现表格翻页功能
2017/10/23 Javascript
前端html中jQuery实现对文本的搜索功能并把搜索相关内容显示出来
2017/11/14 jQuery
原生微信小程序开发中 redux 的使用详解
2021/02/18 Javascript
Python中特殊函数集锦
2015/07/27 Python
python字典多键值及重复键值的使用方法(详解)
2016/10/31 Python
Python设置在shell脚本中自动补全功能的方法
2018/06/25 Python
Python给定一个句子倒序输出单词以及字母的方法
2018/12/20 Python
Opencv实现抠图背景图替换功能
2019/05/21 Python
Python除法之传统除法、Floor除法及真除法实例详解
2019/05/23 Python
Python切片列表字符串如何实现切换
2020/08/06 Python
美国畅销的跑步机品牌:ProForm
2017/02/06 全球购物
马来西亚与新加坡长途巴士售票网站:BusOnlineTicket.com
2018/11/05 全球购物
岗位竞聘演讲稿
2014/01/10 职场文书
学校评语大全
2014/05/06 职场文书
2015年学习部工作总结范文
2015/03/31 职场文书
2015暑期社会实践调查报告
2015/07/14 职场文书
关于Python OS模块常用文件/目录函数详解
2021/07/01 Python
Java面试题冲刺第十五天--设计模式
2021/08/07 面试题
Golang使用Panic与Recover进行错误捕获
2022/03/22 Golang
Python os和os.path模块详情
2022/04/02 Python