Home  >  Article  >  Backend Development  >  Create CSV file from PHP data

Create CSV file from PHP data

WBOY
WBOYOriginal
2016-07-25 08:42:421016browse
  1. function generateCsv($data, $delimiter = ',', $enclosure = '"') {
  2. $handle = fopen('php://temp', 'r+');
  3. foreach ($data as $line) {
  4. fputcsv($handle, $line, $delimiter, $enclosure);
  5. }
  6. rewind($handle);
  7. while (!feof($handle)) {
  8. $contents .= fread($handle, 8192);
  9. }
  10. fclose($handle);
  11. return $contents;
  12. }
复制代码

用法:
  1. $data[0] = "apple";
  2. $data[1] = "oranges";
  3. generateCsv($data, $delimiter = ',', $enclosure = '"');
  4. ?>
复制代码

PHP, 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