Php uses the fputcsv () function to read and write data in csv files

Source: Internet
Author: User
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 about how to read and write data using the fputcsv () function csv file in php, see the following example. 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 delimiter 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' ';}Print" ";}Print'
'.Html entities ($ csv_line [$ I]).'
';
Fclose ($ file) or die ("Can't close file test.csv! ");

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.