2
0

simpleCSV.class.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. class simpleCSV
  3. {
  4. private $_delimiter;
  5. private $_enclosure;
  6. private $_linebreak;
  7. private $_csv = '';
  8. public static function import($filename_or_data, $is_data = false, $delimiter = 'auto', $enclosure = 'auto', $linebreak = 'auto')
  9. {
  10. $csv = new static($delimiter, $enclosure, $linebreak);
  11. return $csv->toArray($filename_or_data, $is_data);
  12. }
  13. public static function export($items, $delimiter = ',', $enclosure = '"', $linebreak = "\r\n")
  14. {
  15. $csv = new static($delimiter, $enclosure, $linebreak);
  16. return $csv->fromArray($items);
  17. }
  18. public function __construct($delimiter = 'auto', $enclosure = 'auto', $linebreak = 'auto')
  19. {
  20. $this->_delimiter = $delimiter;
  21. $this->_enclosure = $enclosure;
  22. $this->_linebreak = $linebreak;
  23. }
  24. public function delimiter($set = false)
  25. {
  26. if ($set !== false) {
  27. return $this->_delimiter = $set;
  28. }
  29. if ($this->_delimiter === 'auto') {
  30. // detect delimiter
  31. if (strpos($this->_csv, $this->_enclosure . ',') !== false) {
  32. $this->_delimiter = ',';
  33. } else if (strpos($this->_csv, $this->_enclosure . "\t") !== false) {
  34. $this->_delimiter = "\t";
  35. } else if (strpos($this->_csv, $this->_enclosure . ';') !== false) {
  36. $this->_delimiter = ';';
  37. } else if (strpos($this->_csv, ',') !== false) {
  38. $this->_delimiter = ',';
  39. } else if (strpos($this->_csv, "\t") !== false) {
  40. $this->_delimiter = "\t";
  41. } else if (strpos($this->_csv, ';') !== false) {
  42. $this->_delimiter = ';';
  43. } else {
  44. $this->_delimiter = ',';
  45. }
  46. }
  47. return $this->_delimiter;
  48. }
  49. public function enclosure($set = false)
  50. {
  51. if ($set !== false) {
  52. return $this->_enclosure = $set;
  53. }
  54. if ($this->_enclosure === 'auto') {
  55. // detect quot
  56. if (strpos($this->_csv, '"') !== false) {
  57. $this->_enclosure = '"';
  58. } else if (strpos($this->_csv, "'") !== false) {
  59. $this->_enclosure = "'";
  60. } else {
  61. $this->_enclosure = '"';
  62. }
  63. }
  64. return $this->_enclosure;
  65. }
  66. public function linebreak($set = false)
  67. {
  68. if ($set !== false) {
  69. return $this->_linebreak = $set;
  70. }
  71. if ($this->_linebreak === 'auto') {
  72. if (strpos($this->_csv, "\r\n") !== false) {
  73. $this->_linebreak = "\r\n";
  74. } else if (strpos($this->_csv, "\n") !== false) {
  75. $this->_linebreak = "\n";
  76. } else if (strpos($this->_csv, "\r") !== false) {
  77. $this->_linebreak = "\r";
  78. } else {
  79. $this->_linebreak = "\r\n";
  80. }
  81. }
  82. return $this->_linebreak;
  83. }
  84. public function toArray($filename, $is_csv_content = false)
  85. {
  86. $this->_csv = $is_csv_content ? $filename : file_get_contents($filename);
  87. $CSV_LINEBREAK = $this->linebreak();
  88. $CSV_ENCLOSURE = $this->enclosure();
  89. $CSV_DELIMITER = $this->delimiter();
  90. $r = array();
  91. $cnt = strlen($this->_csv);
  92. $esc = false;
  93. $i = $k = $n = 0;
  94. $r[$k][$n] = '';
  95. while ($i < $cnt) {
  96. $ch = $this->_csv[$i];
  97. $chch = ($i < $cnt - 1) ? $ch . $this->_csv[$i + 1] : $ch;
  98. if ($ch === $CSV_LINEBREAK) {
  99. if ($esc) {
  100. $r[$k][$n] .= $ch;
  101. } else {
  102. $k++;
  103. $n = 0;
  104. $esc = false;
  105. $r[$k][$n] = '';
  106. }
  107. } else if ($chch === $CSV_LINEBREAK) {
  108. if ($esc) {
  109. $r[$k][$n] .= $chch;
  110. } else {
  111. $k++;
  112. $n = 0;
  113. $esc = false;
  114. $r[$k][$n] = '';
  115. }
  116. $i++;
  117. } else if ($ch === $CSV_DELIMITER) {
  118. if ($esc) {
  119. $r[$k][$n] .= $ch;
  120. } else {
  121. $n++;
  122. $r[$k][$n] = '';
  123. $esc = false;
  124. }
  125. } else if ($chch === $CSV_ENCLOSURE . $CSV_ENCLOSURE && $esc) {
  126. $r[$k][$n] .= $CSV_ENCLOSURE;
  127. $i++;
  128. } elseif ($ch === $CSV_ENCLOSURE) {
  129. $esc = !$esc;
  130. } else {
  131. $r[$k][$n] .= $ch;
  132. }
  133. $i++;
  134. }
  135. return $r;
  136. }
  137. public function fromArray($items)
  138. {
  139. if (!is_array($items)) {
  140. trigger_error('CSV::export array required', E_USER_WARNING);
  141. return false;
  142. }
  143. $CSV_DELIMITER = $this->delimiter();
  144. $CSV_ENCLOSURE = $this->enclosure();
  145. $CSV_LINEBREAK = $this->linebreak();
  146. $result = '';
  147. foreach ($items as $i) {
  148. $line = '';
  149. foreach ($i as $v) {
  150. if (strpos($v, $CSV_ENCLOSURE) !== false) {
  151. $v = str_replace($CSV_ENCLOSURE, $CSV_ENCLOSURE . $CSV_ENCLOSURE, $v);
  152. }
  153. if ((strpos($v, $CSV_DELIMITER) !== false) || (strpos($v, $CSV_ENCLOSURE) !== false) || (strpos($v, $CSV_LINEBREAK) !== false)) {
  154. $v = $CSV_ENCLOSURE . $v . $CSV_ENCLOSURE;
  155. }
  156. $line .= $line ? $CSV_DELIMITER . $v : $v;
  157. }
  158. $result .= $result ? $CSV_LINEBREAK . $line : $line;
  159. }
  160. return $result;
  161. }
  162. }