Php csv operation code

Source: Internet
Author: User

Click Next to download: CSV operation class
The usage instructions are as follows:

1. Generate a CSV file

CopyCode The Code is 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 ID", "student ID", "Student name ");

$ Csvfile = new csvdatafile ("", "W ");
Echo $ csvfile-> printline ($ arr_export_titles );
// Method 1
$ Print_data1 [] = 1;
$ Print_datal [] = "039413301 ";
$ Print_data1 [] = "James ";
Echo $ csvfile-> printline ($ print_data1 );

$ Print_data2 [] = 2;
$ Print_data2 [] = "039413302 ";
$ Print_data2 [] = "Li Si ";
Echo $ csvfile-> printline ($ print_data2 );

$ Print_data3 [] = 3;
$ Print_data3 [] = "039413303 ";
$ Print_data3 [] = "Wang Wu ";
Echo $ csvfile-> printline ($ print_data3 );

// Method 2
$ Print_data [1] [] = 1;
$ Print_data [2] [] = "039413301 ";
$ Print_data [1] [] = "James ";
$ Print_data [2] [] = 2;
$ Print_data [2] [] = "039413302 ";
$ Print_data [2] [] = "Li Si ";
$ Print_data [3] [] = 3;
$ Print_data [3] [] = "039413303 ";
$ Print_data [3] [] = "Wang Wu ";
Echo $ csvfile-> printcsv ($ print_data );

2. Open CSV read data

CodeCopy codeThe Code is 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 (',', str_replace ('"','', trim (@ fgets ($ FD, 1000 ))));
@ Fclose ($ FD );

If ($ first_line! = "Student ID, student ID, Student name "){
$ Pass = false;
}

If ($ pass ){
$ CSV = new csvdatafile ($ filename );
While ($ CSV-> next_row ()){
$ Userid = trim ($ CSV-> F ('student number '));
$ Classno = trim ($ CSV-> F ('student ID '));
$ 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.