PHP CSV operation class Code _php instance

Source: Internet
Author: User
Please click on the following address download: CSV operation class
Instructions for use are as follows:

1. Generate CSV file

Copy Code code as follows:

Require "./include/csvdatafile.php";

Set_time_limit (200);
Header ("content-type:application/rfc822");
Header (' content-disposition:attachment; Filename=export.csv ');

$arr _export_titles = Array ("Student number", "School Number", "Student Name");

$csvfile = new Csvdatafile ("", ",", "w");
echo $csvfile->printline ($arr _export_titles);
Method One
$print _data1[] = 1;
$print _data1[] = "039413301";
$print _data1[] = "John";
echo $csvfile->printline ($print _data1);

$print _data2[] = 2;
$print _data2[] = "039413302";
$print _data2[] = "Dick";
echo $csvfile->printline ($print _data2);


$print _data3[] = 3;
$print _data3[] = "039413303";
$print _data3[] = "Harry";
echo $csvfile->printline ($print _data3);


Method Two
$print _data[1][] = 1;
$print _data[1][] = "039413301";
$print _data[1][] = "John";
$print _data[2][] = 2;
$print _data[2][] = "039413302";
$print _data[2][] = "Dick";
$print _data[3][] = 3;
$print _data[3][] = "039413303";
$print _data[3][] = "Harry";
echo $csvfile->printcsv ($print _data);





2. Open CSV Read data



Code
Copy Code code as follows:

Require "./include/csvdatafile.php";

$filename = "E:/development/csvfile/datefile.csv";

Read File Source
$handle = fopen ($filename, "R");
$contents = Fread ($handle, FileSize ($filename));
Fclose ($handle);

Format content for special chars
$contents = @addslashes ($contents);
$contents = @str_replace (' \, ', ' \, ', $contents);
$contents = @stripslashes ($contents);

Write to new file
$handle = @fopen ($filename, "w");
@fwrite ($handle, $contents);
@fclose ($handle);

$FD = @fopen ($filename, "RB");
$first _line = Str_replace (', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', Trim (@fgets ($FD, 1000)));
@fclose ($FD);

if ($first _line!= "student number, learner number, student name") {
$pass = false;
}

if ($pass) {
$csv = new Csvdatafile ($filename);
while ($csv->next_row ()) {
$userid = Trim ($csv->f (' Student number '));
$classno = Trim ($csv->f (' School Number '));
$username = Trim ($csv->f (' Student name '));
}
}

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.