php使用fputcsv()函數(shù)csv文件讀寫數(shù)據(jù)的方法
本文實例講述了php使用fputcsv()函數(shù)csv文件讀寫數(shù)據(jù)的方法。分享給大家供大家參考。具體分析如下:
fputcsv() 函數(shù)用于將數(shù)據(jù)格式為csv格式,以便寫入文件或者數(shù)據(jù)庫.
1.將字符串寫入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."<br>";
if($isSuccess===false)
{
die("Can't write csv line".$line_array);
}
}
fclose($file) or die("Can't close file test.csv.");
fputcsv()函數(shù)返回所寫入行的字符的個數(shù)或者false,當(dāng)寫入失敗時返回false.
2.將格式化的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")
);
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)函數(shù)讀取csv文件.
fgetcsv的參數(shù)說明如下:
file:需要讀取的csv文件,此參數(shù)是必需的。
length:表示大于csv文件中最長的行的長度的值。php5之前是必需參數(shù)。在php5中是可選參數(shù),如果不設(shè)置此參數(shù)或者將其設(shè)為0,php將會讀取.
一整行的數(shù)據(jù)。如果行的長度超過8192個字節(jié)時,應(yīng)該將length值設(shè)定一個數(shù),而不是讓php自動去計算行的長度。
separator:指定數(shù)據(jù)的分隔符,默認(rèn)是逗號,如果指定為“;”,那么fgetcsv函數(shù)將按照“;”來解析行數(shù)據(jù)。
fgetcsv的返回值:
根據(jù)file的一行數(shù)據(jù),返回一個數(shù)組,如果讀取文件出錯,則返回false,到達(dá)文件尾部時,也返回false.
下面是一個讀取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!");
希望本文所述對大家的php程序設(shè)計有所幫助。
相關(guān)文章
Windows環(huán)境下安裝PHP Pear的方法圖文教程
這篇文章主要介紹了Windows環(huán)境下安裝PHP Pear的方法,結(jié)合圖文形式詳細(xì)說明了Windows環(huán)境下安裝PHP Pear的相關(guān)命令與操作技巧,需要的朋友可以參考下2019-07-07
php session_start()關(guān)于Cannot send session cache limiter - hea
在windows下編程,當(dāng)使用session_start()方法的時候,有時會報 session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/www/inpublisher/php1.php:1)這樣的錯誤2009-11-11
PHP結(jié)合Ffmpeg快速搭建流媒體服務(wù)的實踐記錄
這篇文章主要給大家介紹了關(guān)于使用PHP結(jié)合Ffmpeg快速搭建流媒體服務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10

