Posted By

mscribellito on 04/08/11


Tagged


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

AllKnightAccess
notturnale
czehner


PHP CSV GENERATOR


 / Published in: PHP
 

  1. <?php
  2.  
  3. /*
  4.  * @class: CSV
  5.  * @author: Michael Scribellito
  6.  * @date: 6-1-11
  7.  */
  8.  
  9. class CSV {
  10.  
  11. private $config = array(
  12. 'delimiter' => ',',
  13. 'extension' => 'csv',
  14. 'quoteSpaces' => TRUE,
  15. );
  16. private $headings = array();
  17. private $rows = array();
  18.  
  19. const NEW_LINE = "\n";
  20.  
  21. public function __construct($config = array()) {
  22.  
  23. $this->initialize($config);
  24. }
  25.  
  26. private function initialize($config = array()) {
  27.  
  28. if (is_array($config) && count($config) > 0) {
  29.  
  30. foreach ($config as $key => $val) {
  31. if (array_key_exists($key, $this->config)) {
  32. $this->config[$key] = $val;
  33. }
  34. }
  35. }
  36. }
  37.  
  38. public function setDelimiter($delimiter) {
  39.  
  40. $this->config['delimiter'] = (string) $delimiter;
  41.  
  42. return $this;
  43. }
  44.  
  45. public function setQuoteSpaces($quote = FALSE) {
  46.  
  47. $this->config['quoteSpaces'] = ($quote === TRUE) ? TRUE : FALSE;
  48.  
  49. return $this;
  50. }
  51.  
  52. public function setColumnHeadings($headings) {
  53.  
  54. if (is_array($headings)) {
  55.  
  56. } else if (is_object($headings)) {
  57. $headings = array_values((array) $headings);
  58. } else {
  59. $headings = func_get_args();
  60. }
  61.  
  62. foreach ($headings as $key => $val) {
  63. $headings[$key] = $this->enclose($val);
  64. }
  65.  
  66. $this->headings = $headings;
  67.  
  68. return $this;
  69. }
  70.  
  71. public function addRow($row) {
  72.  
  73. if (is_array($row)) {
  74.  
  75. } else if (is_object($row)) {
  76. $row = array_values((array) $row);
  77. } else {
  78. $row = func_get_args();
  79. }
  80.  
  81. foreach ($row as $key => $val) {
  82. $row[$key] = $this->enclose($val);
  83. }
  84.  
  85. $this->rows[] = $row;
  86.  
  87. return $this;
  88. }
  89.  
  90. public function removeRow($index) {
  91.  
  92. if (is_int($index)) {
  93. unset($this->rows[$index]);
  94. }
  95.  
  96. return $this;
  97. }
  98.  
  99. public function addData($data) {
  100.  
  101. if (!is_array($data) || count($data) == 0) {
  102. return FALSE;
  103. }
  104.  
  105. foreach ($data as $row) {
  106. $this->addRow($row);
  107. }
  108.  
  109. return $this;
  110. }
  111.  
  112. private function enclose($value) {
  113.  
  114. if (preg_match('/[
  115. \t]/', $value)) {
  116. // if there are new lines or tabs
  117. $value = '"' . $value . '"';
  118. } else if (preg_match('/"/', $value)) {
  119. // if there is a quote in the field
  120. $value = '"' . str_replace('"', '""', $value) . '"';
  121. } else {
  122. if (preg_match('/,/', $value)) {
  123. // if there is a comma in the field
  124. $value = '"' . $value . '"';
  125. } else if ((preg_match('/\s+/', $value)) && $this->config['quoteSpaces'] === TRUE) {
  126. // if there is a space before or after
  127. $value = '"' . $value . '"';
  128. }
  129. }
  130.  
  131. return $value;
  132. }
  133.  
  134. public function generate() {
  135.  
  136. $csv = '';
  137.  
  138. if (count($this->headings) > 0) {
  139.  
  140. $csv .= implode($this->config['delimiter'], $this->headings) . self::NEW_LINE;
  141. }
  142.  
  143. if (count($this->rows) > 0) {
  144.  
  145. foreach ($this->rows as $row) {
  146.  
  147. $csv .= implode($this->config['delimiter'], $row) . self::NEW_LINE;
  148. }
  149. }
  150.  
  151. $this->clear();
  152.  
  153. return $csv;
  154. }
  155.  
  156. public function clear() {
  157.  
  158. $this->headings = array();
  159. $this->rows = array();
  160.  
  161. return $this;
  162. }
  163.  
  164. public function __toString() {
  165.  
  166. return $this->generate();
  167. }
  168.  
  169. public function output($type = 'I', $filename = '') {
  170.  
  171. $type = strtoupper($type);
  172.  
  173. if (empty($filename)) {
  174. $filename = time() . '.' . $this->config['extension'];
  175. } else {
  176. $filename .= '.' . $this->config['extension'];
  177. }
  178.  
  179. $csv = $this->generate();
  180.  
  181. switch ($type) {
  182.  
  183. case 'I':
  184.  
  185. if (!headers_sent()) {
  186.  
  187. header('Content-Type: text/plain');
  188. header('Content-Length: ' . strlen($csv));
  189. header('Content-Disposition: inline; filename=' . $filename . '');
  190.  
  191. echo $csv;
  192. }
  193.  
  194. break;
  195.  
  196. case 'D':
  197.  
  198. if (!headers_sent()) {
  199.  
  200. header('Content-Type: text/csv');
  201. header('Content-Length: ' . strlen($csv));
  202. header('Content-Disposition: attachment; filename="' . $filename . '"');
  203. header('Cache-Control: private, max-age=0, must-revalidate');
  204. header('Pragma: public');
  205.  
  206. echo $csv;
  207. }
  208.  
  209. break;
  210.  
  211. case 'F':
  212.  
  213. break;
  214.  
  215. case 'S':
  216.  
  217. return $this->buffer;
  218. }
  219. }
  220.  
  221. }
  222.  
  223. ?>

Report this snippet  

You need to login to post a comment.