This article mainly introduces how to read and write data using the fputcsv () function csv file in php, and analyzes the skills of the fputcsv () function for reading and writing csv files, which has some reference value, for more information, see
This article mainly introduces how to read and write data using the fputcsv () function csv file in php, and analyzes the skills of the fputcsv () function for reading and writing csv files, which has some reference value, for more information, see
This example describes how to read and write data using the fputcsv () function csv file in php. Share it with you for your reference. The specific analysis is as follows:
The fputcsv () function is used to write data into a file or database in csv format.
1. Write the string to the csv file. The Code is as follows:
The Code is as follows:
$ Test_array = array (
Array ("111", "sdfsd", "sdds", "43344", "rrrr "),
Array ("sssssssss", "gdfgd", "232323", "wwewe", "dsfds "),
Array ("fgg", "e4343", "dsfds", "w2332", "xcvxc "),
Array ("11212", "2323", "344343", "344343", "rerreer "),
Array ("fds", "43344444", "33333333", "ttttttt", "gggggggggg "),
Array ("kdfs", "dsfdsfds", "wewewe", "sdsdddddddddd", "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 .");
The fputcsv () function returns the number of characters in the written row or false. If the write fails, false is returned.
2. Save the formatted csv string to the string. The Code is as follows:
The Code is as follows:
$ Test_array = array (
Array ("111", "sdfsd", "sdds", "43344", "rrrr "),
Array ("sssssssss", "gdfgd", "232323", "wwewe", "dsfds "),
Array ("fgg", "e4343", "dsfds", "w2332", "xcvxc "),
Array ("11212", "2323", "344343", "344343", "rerreer "),
Array ("fds", "43344444", "33333333", "ttttttt", "gggggggggg "),
Array ("kdfs", "dsfdsfds", "wewewe", "sdsdddddddddd", "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 ();
Use the fgetcsv (file, length, separator, enclosure) function to read the csv file.
Fgetcsv parameters are described as follows:
File: csv file to be read. this parameter is required.
Length: The value greater than the length of the longest row in the csv file. Php5 is a required parameter. It is an optional parameter in php5. If you do not set this parameter or set it to 0, php will read it.
A whole row of data. If the length of a row exceeds 8192 bytes, set the length value to a number instead of letting php automatically calculate the length of the row.
Separator: Specifies the Data separator. The default value is a comma. If it is specified as ";", the fgetcsv function parses row data according.
Return Value of fgetcsv:
Returns an array based on a row of data in the file. If an error occurs while reading the file, false is returned. When the object is reached, false is returned.
The following is an example of reading the test.csv file:
The Code is 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'
'.Html entities ($ csv_line [$ I]).' | ';}Print"
";}Print'
';
Fclose ($ file) or die ("Can't close file test.csv! ");
I hope this article will help you with php programming.