Phpcsv operation class implementation code. For more information, see. The usage instructions are as follows:
1. generate a csv file
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_data1 [] = "039413301"; $ print_data1 [] = "Zhangsan "; 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 [1] [] = "039413301 "; $ print_data [1] [] = "James"; $ print_data [2] [] = 2; $ print_data [2] [] = "039413302 "; $ print_data [2] [] = ""; $ print_data [3] [] = 3; $ print_data [3] [] = "039413303 "; $ print_data [3] [] = "Wang Wu"; echo $ csvfile-> printcsv ($ print_data );
2. open csv read data
Code
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 = @ fo Pen ($ 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 '));}}
For more articles about the php csv operation code, refer to the PHP Chinese website!