《PHP應(yīng)用:php使用fputcsv()函數(shù)csv文件讀寫數(shù)據(jù)的方法》要點:
本文介紹了PHP應(yīng)用:php使用fputcsv()函數(shù)csv文件讀寫數(shù)據(jù)的方法,希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP編程本文實例講述了php使用fputcsv()函數(shù)csv文件讀寫數(shù)據(jù)的辦法.分享給大家供大家參考.具體分析如下:
PHP編程fputcsv() 函數(shù)用于將數(shù)據(jù)格式為csv格式,以便寫入文件或者數(shù)據(jù)庫.
PHP編程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()函數(shù)返回所寫入行的字符的個數(shù)或者false,當(dāng)寫入失敗時返回false.
PHP編程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);
??????? }
}
PHP編程fclose($file) or die("Can't close file test.csv.");
$result = ob_get_contents();
ob_end_clean();
以用fgetcsv(file,length,separator,enclosure)函數(shù)讀取csv文件.
PHP編程fgetcsv的參數(shù)說明如下:
PHP編程file:需要讀取的csv文件,此參數(shù)是必需的.
PHP編程length:表示大于csv文件中最長的行的長度的值.php5之前是必需參數(shù).在php5中是可選參數(shù),如果不設(shè)置此參數(shù)或者將其設(shè)為0,php將會讀取.
PHP編程一整行的數(shù)據(jù).如果行的長度超過8192個字節(jié)時,應(yīng)該將length值設(shè)定一個數(shù),而不是讓php自動去計算行的長度.
PHP編程separator:指定數(shù)據(jù)的分隔符,默認是逗號,如果指定為“;”,那么fgetcsv函數(shù)將依照“;”來解析行數(shù)據(jù).
PHP編程fgetcsv的返回值:
PHP編程根據(jù)file的一行數(shù)據(jù),返回一個數(shù)組,如果讀取文件出錯,則返回false,到達文件尾部時,也返回false.
PHP編程下面是一個讀取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!");
PHP編程希望本文所述對大家的php程序設(shè)計有所贊助.
維易PHP培訓(xùn)學(xué)院每天發(fā)布《PHP應(yīng)用:php使用fputcsv()函數(shù)csv文件讀寫數(shù)據(jù)的方法》等實戰(zhàn)技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養(yǎng)人才。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/12822.html