PHP fputcsv () function CSV data read/write database file code _php tutorial

Source: Internet
Author: User
PHP tutorial Fputcsv () function CSV data read/write database tutorial file code

The Fputcsv () function is used to format the data in CSV format for writing to a file or database.

1. Writing a string to the CSV file

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

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 written to the row or false, which returns false when the write fails.

2. Save the formatted CSV string to a string.

$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, if you do not set this parameter or set it to 0,php will read

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 there is an error reading the file. Also returns false when the end of the file is reached.

The following is an example of reading a 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 '
'. Htmlentities ($csv _line[$i]). '
';
Fclose ($file) or Die ("Can ' t close file test.csv!");


http://www.bkjia.com/PHPjc/444771.html www.bkjia.com true http://www.bkjia.com/PHPjc/444771.html techarticle PHP Tutorial Fputcsv () function CSV data read/write database tutorial file code fputcsv () function is used to format the data in CSV format for writing to a file or database. 1. Write a string to CS ...

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