zoukankan      html  css  js  c++  java
  • 将字符串格式化为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();

  • 相关阅读:
    python中的map,fliter,reduce用法
    python中的函数参数传递
    python中的全局变量和局部变量
    python中的函数定义
    python中的eval()和exec()函数
    kafka手动提交,丢失数据
    02-基本概念
    01-接触kafka
    (8)适配模式--结构性
    java内存划分
  • 原文地址:https://www.cnblogs.com/ywxgod/p/2057383.html
Copyright © 2011-2022 走看看