Phpfputcsv () function csv data read/write database file code _ PHP Tutorial

Source: Internet
Author: User
Phpfputcsv () function csv data read and write database file code. Php Tutorial fputcsv () function csv data read/write database tutorial file code fputcsv () function is used to convert the data format to csv format for writing files or databases. 1. write the string to the cs php Tutorial fputcsv () function csv data read/write database tutorial file code

The fputcsv () function is used to write data into a file or database in csv format.

1. write the string to the csv file.

$ 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.

$ 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

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 end of the file is reached, false is also returned.

The following is an example of reading the test.csv file.

$ 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! ");


Fputcsv () function csv data read/write database tutorial file code fputcsv () function is used to convert the data format to csv format for writing files or databases. 1. write the string to cs...

Related Article

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.