CSV.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. class PHPExcel_Reader_CSV implements Iterator, Countable {
  3. private $_fileHandle = null;
  4. private $filePath = '';
  5. private $_inputEncoding = 'UTF-8';
  6. private $_delimiter = ',';
  7. private $_enclosure = '"';
  8. private $_filter = 0;
  9. /**
  10. * @param string $filePath
  11. * @param int $filter filter empty row
  12. *
  13. * @throws Exception
  14. */
  15. public function __construct($filePath, $filter = 0) {
  16. if (! file_exists($filePath)) {
  17. throw new Exception("Could not open " . $filePath . " for reading! File does not exist.");
  18. }
  19. $this->filePath = $filePath;
  20. $this->_filter = $filter;
  21. ini_set('auto_detect_line_endings', true);
  22. $this->_fileHandle = fopen($filePath, 'r');
  23. $this->_detectEncoding();
  24. }
  25. /**
  26. * Move filepointer past any BOM marker
  27. */
  28. private function _detectEncoding() {
  29. $step = $BOMLength = 0;
  30. while ($step < 3) {
  31. $BOM = bin2hex(fread($this->_fileHandle, 2 + $step++));
  32. rewind($this->_fileHandle);
  33. if ($BOM == 'fffe' || $BOM == 'feff') {
  34. $BOMLength = 2;
  35. $this->_delimiter = "\t";
  36. $this->_inputEncoding = 'UTF-16';
  37. break;
  38. } else {
  39. if ($BOM == 'efbbbf') {
  40. $BOMLength = 3;
  41. break;
  42. } else {
  43. if ($BOM == '0000feff' || $BOM == 'fffe0000') {
  44. $BOMLength = 4;
  45. $this->_delimiter = "\t";
  46. $this->_inputEncoding = 'UTF-32';
  47. break;
  48. }
  49. }
  50. }
  51. }
  52. if (! $BOMLength) {
  53. $encoding = mb_detect_encoding(fgets($this->_fileHandle, 1024), 'ASCII, UTF-8, GB2312, GBK');
  54. rewind($this->_fileHandle);
  55. if ($encoding) {
  56. if ($encoding == 'EUC-CN') {
  57. $this->_inputEncoding = 'GB2312';
  58. } else {
  59. if ($encoding == 'CP936') {
  60. $this->_inputEncoding = 'GBK';
  61. } else {
  62. $this->_inputEncoding = $encoding;
  63. }
  64. }
  65. }
  66. }
  67. if ($this->_inputEncoding != 'UTF-8') {
  68. stream_filter_register("convert_iconv.*", "convert_iconv_filter");
  69. stream_filter_append($this->_fileHandle, 'convert_iconv.' . $this->_inputEncoding . '/UTF-8');
  70. }
  71. }
  72. /**
  73. * Returns information about sheets in the file.
  74. * @return array
  75. */
  76. public function Sheets() {
  77. return array(0 => basename($this->filePath));
  78. }
  79. /**
  80. * Changes sheet to another.
  81. *
  82. * @param int $index
  83. * @return bool
  84. */
  85. public function ChangeSheet($index) {
  86. if ($index == 0) {
  87. $this->rewind();
  88. return true;
  89. }
  90. return false;
  91. }
  92. /**
  93. * Rewind the Iterator to the first element.
  94. */
  95. public function rewind() {
  96. rewind($this->_fileHandle);
  97. $this->currentRow = null;
  98. $this->index = 0;
  99. }
  100. /**
  101. * Return the current element.
  102. * @return mixed
  103. */
  104. public function current() {
  105. if ($this->index == 0 && ! isset($this->currentRow)) {
  106. $this->rewind();
  107. $this->next();
  108. $this->index = 0;
  109. }
  110. return $this->currentRow;
  111. }
  112. /**
  113. * Move forward to next element.
  114. */
  115. public function next() {
  116. $this->currentRow = array();
  117. $this->index++;
  118. while (($row = fgetcsv($this->_fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== false) {
  119. if (! $this->_filter || array_filter($row, array($this, 'filter'))) {
  120. $this->currentRow = $row;
  121. break;
  122. }
  123. }
  124. return $this->currentRow;
  125. }
  126. /**
  127. * Return the identifying key of the current element.
  128. * @return mixed
  129. */
  130. public function key() {
  131. return $this->index;
  132. }
  133. /**
  134. * Check if there is a current element after calls to rewind() or next().
  135. * @return bool
  136. */
  137. public function valid() {
  138. if ($this->currentRow || ! feof($this->_fileHandle)) {
  139. return true;
  140. } else {
  141. fclose($this->_fileHandle);
  142. return false;
  143. }
  144. }
  145. /**
  146. * return the count of the contained items
  147. * @return int
  148. */
  149. public function count() {
  150. if (! isset($this->rowCount)) {
  151. $total = 0;
  152. rewind($this->_fileHandle);
  153. while (($row = fgetcsv($this->_fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== false) {
  154. if (! $this->_filter || array_filter($row, array($this, 'filter'))) {
  155. $total++;
  156. }
  157. }
  158. $this->rowCount = $total;
  159. }
  160. return $this->rowCount;
  161. }
  162. /**
  163. * filter empty string
  164. *
  165. * @param mixed $value
  166. *
  167. * @return boolean
  168. */
  169. private function filter($value) {
  170. return trim($value) !== '';
  171. }
  172. }
  173. class convert_iconv_filter extends php_user_filter {
  174. private $modes;
  175. function filter($in, $out, &$consumed, $closing) {
  176. while ($bucket = stream_bucket_make_writeable($in)) {
  177. $bucket->data = mb_convert_encoding($bucket->data, $this->modes[1], $this->modes[0]);
  178. $consumed += $bucket->datalen;
  179. stream_bucket_append($out, $bucket);
  180. }
  181. return PSFS_PASS_ON;
  182. }
  183. function onCreate() {
  184. $format = explode('/', substr($this->filtername, 14));
  185. if (count($format) == 2) {
  186. $this->modes = $format;
  187. return true;
  188. } else {
  189. return false;
  190. }
  191. }
  192. }