PHP uses the Fputcsv () function CSV file to read and write data,
This example describes how PHP uses the Fputcsv () function CSV file to read and write data. Share to everyone for your reference. The specific analysis is as follows:
The Fputcsv () function is used to format the data in CSV format for writing to a file or database.
1. Write the string to the CSV file with the following code:
Copy code code as follows: $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); The
Print $isSuccess.
";
if ($isSuccess ===false)
{
Die ("Can ' t-write CSV line". $line _array);
}
}
fclose ($file) or Die ("Can ' t close file test.csv."); The
Fputcsv () function returns the number of characters or false for the row being written, and returns False if the write fails.
2. Save the formatted CSV string to a string with the following code:
Copy the Code code as follows: $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 ();
To read the CSV file using the Fgetcsv (file,length,separator,enclosure) function.
The parameters of the fgetcsv are described below:
File: The CSV file that needs to be read, this parameter is required.
Length: Represents the value that is greater than the length of the longest row in the CSV file. PHP5 is a required parameter before. is an optional parameter in PHP5, and will be read if you do not set this parameter or set it to 0,php.
A whole row of data. If the length of the row exceeds 8,192 bytes, you should set the length value to a number instead of letting PHP automatically calculate the length of the line.
Separator: Specifies the delimiter for the data, the default is a comma, and if specified as ";", then the Fgetcsv function will parse the row data according to ";".
return value of Fgetcsv:
Returns an array based on a row of file data, returns False if a file error is read, and returns False when the end of the file is reached.
Here is an example of reading a test.csv file:
Copy the code as follows: $file = fopen (' test.csv ', ' r ') or Die ("Can ' t Open file Test.csv");
$color = "#ff0000";
print '
'; while ($csv _line=fgetcsv ($file)) {print "
"; $len = count ($csv _line); for ($i =0; $i < $len; $i + +) {if ($i%2==0) $color = "#cccccc"; else $color = "#999999"; print '
'. Htmlentities ($csv _line[$i]). ' | '; } print "
"; } print '
';
Fclose ($file) or Die ("Can ' t close file test.csv!");
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/938852.html www.bkjia.com true http://www.bkjia.com/PHPjc/938852.html techarticle PHP uses the fputcsv () function CSV file to read and write data, the example of this article describes PHP using the Fputcsv () function CSV file read and write data method. Share to everyone for your reference. A specific analysis such as ...