PHP uses the Fputcsv () function CSV file to read and write data method _php tips

Source: Internet
Author: User

This example describes the way 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 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 ("A", "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. " <br> ";
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 line or false, which returns false when the write fails.

2. Save the formatted CSV string to a string with the following code:

Copy Code code as follows:
$test _array = Array (
Array ("A", "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 with 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 a value that is longer than the longest row in the CSV file. Required parameters before php5. An optional parameter in PHP5 is read if you do not set this parameter or set it to 0,php.

An entire row of data. If the line is longer than 8,192 bytes, you should set the length value to a number instead of having PHP automatically calculate the length of the row.

Separator: Specifies the delimiter of the data, the default is a comma, and if specified as ";", then the Fgetcsv function parses the row data according to ";".

Fgetcsv return Value:

Returns an array of files based on one row of data, False if the file is read, or False when the file is at the tail.

The following is an example of reading a test.csv file:

Copy Code code as follows:
$file = fopen (' test.csv ', ' r ') or Die ("Can ' t Open file Test.csv");
$color = "#ff0000";
print ' <table border=0> ';
while ($csv _line=fgetcsv ($file))
{
print "<tr>";
$len = count ($csv _line);
for ($i =0; $i < $len; $i + +)
{
if ($i%2==0) $color = "#cccccc";
else $color = "#999999";
print ' <td bgcolor= '. $color. ' > '. Htmlentities ($csv _line[$i]). ' </td> ';
}
print "</tr>";
}
print ' </table> ';
Fclose ($file) or Die ("Can ' t close file test.csv!");

I hope this article will help you with your PHP program design.

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.