php使用fputcsv()函数csv文件读写数据的方法,_PHP教程
程序员文章站
2022-06-16 19:19:03
...
php使用fputcsv()函数csv文件读写数据的方法,
本文实例讲述了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."
";
if($isSuccess===false)
{
die("Can't write csv line".$line_array);
}
}
fclose($file) or die("Can't close file test.csv.");
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."
";
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);
}
}
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 '';
';
fclose($file) or die("Can't close file test.csv!");
$color="#ff0000";
print '
'.htmlentities($csv_line[$i]).' | ';
fclose($file) or die("Can't close file test.csv!");
希望本文所述对大家的php程序设计有所帮助。
推荐阅读
-
PHP使用mysql_fetch_row查询获得数据行列表的方法,phpmysql_fetch_row_PHP教程
-
php+mysqli使用预处理技术进行数据库查询的方法,mysqli预处理_PHP教程
-
使用PHPMYADMIN操作mysql数据库添加新用户和数据库的方法_PHP教程
-
PHP自动载入类文件函数__autoload的使用方法
-
PHP自动载入类文件函数__autoload的使用方法
-
php教程之魔术方法的使用示例(php魔术函数)
-
php将csv文件导入到mysql数据库的方法
-
php中使用PHPExcel读写excel(xls)文件的方法
-
PHP使用get_headers函数判断远程文件是否存在的方法
-
PHP使用glob函数遍历目录或文件夹的方法