zoukankan      html  css  js  c++  java
  • 从CSV文件中读取数据

    可以用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!");

    最后的输出结果:

    111 sdfsd sdds 43344 rrrr
    sssssssss gdfgfd 232323 wwewe dsfds
    fgfg e4343 dsfds w2332 xcvxc
    11212 2323 344343 344343 rerreer
    fds 43344444 33333333 ttttttt gggggggggggg
    kdfs dsfdsfds wewewe sdsdddddddd wwwwwwwwwww
  • 相关阅读:
    最近积累的JS 东西,分享一下
    C#定时任务框架Quartz.NET
    如何成为微软社区MVP以及年终总结
    git 基于某个分支创建分支
    iframe跨域-Js通信的一种方式
    tcp连接建立断开过程及状态变化
    MySQL-InnoDB的事务隔离与锁
    MySQL索引原理总结
    php gd实现简单图片验证码与图片背景文字水印
    php 取post数据的三种方式
  • 原文地址:https://www.cnblogs.com/ywxgod/p/2057422.html
Copyright © 2011-2022 走看看