Phpcsv operation code

Source: Internet
Author: User
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!

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.