当前位置:首页 > PHP教程 > php文件操作 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-05 20:43:28 浏览: 评论:0 

这篇文章主要介绍了php使用fputcsv()函数csv文件读写数据的方法,分析了fputcsv()函数针对csv文件的读写操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下

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

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

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

  1. $test_array = array( 
  2.     array("111","sdfsd","sdds","43344","rrrr"), 
  3.     array("sssssssss","gdfgfd","232323","wwewe","dsfds"), 
  4.     array("fgfg","e4343","dsfds","w2332","xcvxc"), 
  5.     array("11212","2323","344343","344343","rerreer"), 
  6.     array("fds","43344444","33333333","ttttttt","gggggggggggg"), 
  7.     array("kdfs","dsfdsfds","wewewe","sdsdddddddd","wwwwwwwwwww") 
  8. ); 
  9.  
  10. $file = fopen("test.csv","w") or die("Can't Open test.csv"); 
  11. foreach($test_array as $line_array) 
  12. { 
  13.     $isSuccess = fputcsv($file,$line_array); 
  14.     print $isSuccess."<br>"; 
  15.  if($isSuccess===false) 
  16.     { 
  17.         die("Can't write csv line".$line_array); 
  18.     } 
  19. } 
  20. fclose($file) or die("Can't close file test.csv."); 

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

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

  1. $test_array = array( 
  2.         array("111","sdfsd","sdds","43344","rrrr"), 
  3.         array("sssssssss","gdfgfd","232323","wwewe","dsfds"), 
  4.         array("fgfg","e4343","dsfds","w2332","xcvxc"), 
  5.         array("11212","2323","344343","344343","rerreer"), 
  6.         array("fds","43344444","33333333","ttttttt","gggggggggggg"), 
  7.         array("kdfs","dsfdsfds","wewewe","sdsdddddddd","wwwwwwwwwww") 
  8. ); 
  9. ob_start(); 
  10. $file = fopen("php://output","w") or die("Can't Open php://output"); 
  11. foreach($test_array as $line_array) 
  12. { 
  13.         $isSuccess = fputcsv($file,$line_array); 
  14.         if($isSuccess===false) 
  15.         { 
  16.             die("Can't write csv line".$line_array); 
  17.         } 
  18. } 
  19. fclose($file) or die("Can't close file test.csv."); 
  20. $result = ob_get_contents(); 
  21. 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文件的例子:

  1. $file = fopen('test.csv','r') or die("Can't open file test.csv"); 
  2. $color="#ff0000"; 
  3. print '<table border=0>'; 
  4. while($csv_line=fgetcsv($file)) 
  5. { 
  6.         print "<tr>"; 
  7.         $len = count($csv_line); 
  8.         for($i=0;$i<$len;$i++) 
  9.         { 
  10.             if($i%2==0)$color="#cccccc"; 
  11.             else $color="#999999"; 
  12.             print '<td bgcolor='.$color.'>'.htmlentities($csv_line[$i]).'</td>'; 
  13.         } 
  14.         print "</tr>"; 
  15. } 
  16. print '</table>'; 
  17. fclose($file) or die("Can't close file test.csv!");

Tags: fputcsv csv

分享到: