fputcsv 和 fgetcsv 的使用

来源:互联网 发布:个人淘宝网店怎么申请 编辑:程序博客网 时间:2024/06/08 13:42
//生成逗号分割的数据$sales = array(    array('northeast','2017-4-4',12.52),    array('northeast','2017-4-4',5645.5),    array('southeast','2017-4-4',1.52),    array('southeast','2017-4-4',55.52),);//$fh = fopen('sales.csv','w') or die("can't open sales.csv");ob_start();//打开输出缓冲$fh = fopen('php://output','w');foreach($sales as $sale_line){    if(fputcsv($fh,$sale_line) == false){        die("can't write file");    }}fclose($fh) or die("can't open php://output");$output = ob_get_contents();//获取缓冲区的内容file_put_contents('buffer.txt', $output);ob_end_clean();// 清空(擦除)缓冲区并关闭输出缓冲
//解析逗号分隔的数据
$fp = fopen('sales.csv','r') or die ("can't open this file");print "<table>\n";while($csv_line = fgetcsv($fp)){    print "<tr>";    for($i=0;$i<count($csv_line);$i++){        print '<td>'.htmlentities($csv_line[$i]).'</td>';//htmlentities()函数把字符转换为 HTML 实体。    }    print "</tr>";}print "</table>";fclose($fp) or die("can't close this file");


0 0
原创粉丝点击