您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)php中的csv文件怎么利用fputcsv()函數(shù)進(jìn)行讀取,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
fputcsv() 函數(shù)用于將數(shù)據(jù)格式為csv格式,以便寫(xiě)入文件或者數(shù)據(jù)庫(kù).
1.將字符串寫(xiě)入csv文件中,代碼如下:
復(fù)制代碼 代碼如下:
$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ù)返回所寫(xiě)入行的字符的個(gè)數(shù)或者false,當(dāng)寫(xiě)入失敗時(shí)返回false.
2.將格式化的csv字符串保存到字符串中,代碼如下:
復(fù)制代碼 代碼如下:
$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();
以用fgetcsv(file,length,separator,enclosure)函數(shù)讀取csv文件.
fgetcsv的參數(shù)說(shuō)明如下:
file:需要讀取的csv文件,此參數(shù)是必需的。
length:表示大于csv文件中最長(zhǎng)的行的長(zhǎng)度的值。php5之前是必需參數(shù)。在php5中是可選參數(shù),如果不設(shè)置此參數(shù)或者將其設(shè)為0,php將會(huì)讀取.
一整行的數(shù)據(jù)。如果行的長(zhǎng)度超過(guò)8192個(gè)字節(jié)時(shí),應(yīng)該將length值設(shè)定一個(gè)數(shù),而不是讓php自動(dòng)去計(jì)算行的長(zhǎng)度。
separator:指定數(shù)據(jù)的分隔符,默認(rèn)是逗號(hào),如果指定為“;”,那么fgetcsv函數(shù)將按照“;”來(lái)解析行數(shù)據(jù)。
fgetcsv的返回值:
根據(jù)file的一行數(shù)據(jù),返回一個(gè)數(shù)組,如果讀取文件出錯(cuò),則返回false,到達(dá)文件尾部時(shí),也返回false.
下面是一個(gè)讀取test.csv文件的例子:
復(fù)制代碼 代碼如下:
$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中的csv文件怎么利用fputcsv()函數(shù)進(jìn)行讀取了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。