Home  >  Article  >  Backend Development  >  A small example of php exporting (generating) CSV files

A small example of php exporting (generating) CSV files

WBOY
WBOYOriginal
2016-07-25 08:55:06865browse
  1. /**
  2. * Generate CSV files separated by commas by default
  3. * Solution: The content contains commas (,), double quotes ("")
  4. * @author zf Edit: bbs.it-home.org
  5. * @version 2012-11- 14
  6. */
  7. header("Content-Type: application/vnd.ms-excel; charset=GB2312");
  8. header("Content-Disposition: attachment; filename=CSV data.csv ");
  9. $rs = array(
  10. array('aa', "I'm li lei", '"boy"', '¥122,300.00'),
  11. array('cc', 'I'm han mei', '"gile"', '¥122,500.00'),
  12. );
  13. $str = '';
  14. foreach ($rs as $row) {
  15. $str_arr = array();
  16. foreach ($row as $column) {
  17. $str_arr[] = '"' . str_replace('"', '""', $column) . '"';
  18. }
  19. $str.=implode(',', $str_arr) .PHP_EOL;
  20. }
  21. echo $str;
Copy code

Note: Sometimes it is important to pay attention to the encoding issues when exporting csv files with PHP. >>> More about php exporting csv.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn