This article mainly introduces the summary of php csv file code instances. This article provides six sample code for operating CSV files, including reading
This article mainly introduces the summary of php csv file code instances. This article provides six sample code for operating CSV files, including reading
1. Read csv data and output it to the sales.csv file:
$ Sales = array ('northeast ', '2017-01-01', '2017-02-01 ', 2004), array ('northwest', '2017-01-01 ', '2014-02-01 ', 2004), array ('southeast', '2014-01-01 ', '2014-02-01', 546.33), array ('southwest ', '2017-01-01 ', '2017-02-01', 2004), array ('all regions', '---', '--', 2004 ),); $ fh = fopen('sales.csv ', 'w') or die ("Can't open sales.csv"); foreach ($ sales as $ sales_line) {if (fputcsv ($ fh, $ sales_line) === false) {die ("Can't write CSV line") ;}} fclose ($ fh) or die ("Can't close sales.csv ");
2. Read csv data and use special stream output
$ Sales = array ('northeast ', '2017-01-01', '2017-02-01 ', 2004), array ('northwest', '2017-01-01 ', '2014-02-01 ', 2004), array ('southeast', '2014-01-01 ', '2014-02-01', 546.33), array ('southwest ', '2017-01-01 ', '2017-02-01', 2004), array ('all regions', '---', '--', 2004 ),); $ fh = fopen ('php: // output', 'w'); foreach ($ sales as $ sales_line) {if (fputcsv ($ fh, $ sales_line) === false) {die ("Can't write CSV line") ;}} fclose ($ fh );
3. Read csv data and output it to the buffer.
$ Sales = array ('northeast ', '2017-01-01', '2017-02-01 ', 2004), array ('northwest', '2017-01-01 ', '2014-02-01 ', 2004), array ('southeast', '2014-01-01 ', '2014-02-01', 546.33), array ('southwest ', '2017-01-01 ', '2017-02-01', 2004), array ('all regions', '---', '--', 2004),); ob_start (); $ fh = fopen ('php: // output', 'w') or die ("Can't open php: // output "); foreach ($ sales as $ sales_line) {if (fputcsv ($ fh, $ sales_line) ===false) {die ("Can't write CSV line ");}} fclose ($ fh) or die ("Can't close php: // output"); $ output = ob_get_contents (); ob_end_clean ();
4. Read csv file data
$ Fp = fopen('sample3.csv ', 'R') or die ("can't open file"); print"
\ N "; while ($ csv_line = fgetcsv ($ fp) {print'
'; For ($ I = 0, $ j = count ($ csv_line); $ I <$ j; $ I ++) {// print'
'.Html entities ($ csv_line [$ I]).' | '; Print'
'.Html entities (iconv ("gb2312", "UTF-8", $ csv_line [$ I]).' | ';} Print"
\ N ";} print"
\ N "; fclose ($ fp) or die (" can't close file ");
5. Download the CSV file
$ Sales = array ('northeast ', '2017-01-01', '2017-02-01 ', 2004), array ('northwest', '2017-01-01 ', '2014-02-01 ', 2004), array ('southeast', '2014-01-01 ', '2014-02-01', 546.33), array ('southwest ', '2017-01-01 ', '2017-02-01', 2004), array ('China', '2017-01-01 ', '2017-02-01', 2004 ),); $ fh = fopen ('php: // output', 'w') or die ("can't open php: // output"); $ total = 0; // tell the browser to send a csv file header ('content-Type: application/csv'); header ('content-Disposition: attachment; filename = "sales.csv "'); // output header fputcsv ($ output, array ('region ', 'start date', 'end date', 'amount'); // output each row of data, increment $ totalforeach ($ sales as $ sales_line) {if (fputcsv ($ fh, $ sales_line) === false) {die ("Can't write CSV line");} else {$ total + = $ sales_line [3] ;}} fputcsv ($ fh, array ('all regions ', '--', '--', $ total); fclose ($ fh) or die ("Can't close php: // output ");
6. Read the specified row and interval rows of the CSV file.
/***** Read the specified row in the CSV file *****/function get_file_line_a ($ file_name, $ line) {$ n = 0; $ handle = fopen ($ file_name, 'R'); if ($ handle) {while (! Feof ($ handle) {++ $ n; $ out = fgets ($ handle, 4096); if ($ line = $ n) break ;} fclose ($ handle);} if ($ line = $ n) return $ out; return false;} echo get_file_line ("windows_201511s.csv", 10 ); // enter 10th rows/***** read the range row in the CSV file *****/function get_file_line_ B ($ file_name, $ line_star, $ line_end) {$ n = 0; $ handle = fopen ($ file_name, "r"); if ($ handle) {while (! Feof ($ handle) {+ $ n; $ out = fgets ($ handle, 4096); if ($ line_star <= $ n) {$ ling [] = $ out;} if ($ line_end = $ n) break;} fclose ($ handle);} if ($ line_end = $ n) return $ ling; return false;} // use get_file_line to read and output 11th rows to 20th rows $ aa = get_file_line ("windows_201511s.csv", 11, 20 ); // from row 11th to row 20th foreach ($ aa as $ bb) {echo $ bb."
";}
,