Last active
August 29, 2015 14:23
-
-
Save nimmneun/2f92e4979408bda85e44 to your computer and use it in GitHub Desktop.
Simple php csv reader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * @author nimmneun | |
| * @since 27.06.2015 06:05 | |
| */ | |
| class CsvReader | |
| { | |
| /** | |
| * The single line strings. | |
| * @var array | |
| */ | |
| private $lines; | |
| /** | |
| * Character to seperate data within a line. | |
| * @var string | |
| */ | |
| private $delimiter; | |
| /** | |
| * Character enclosing data. | |
| * @var string | |
| */ | |
| private $enclose; | |
| /** | |
| * The current line number. | |
| * @var int | |
| */ | |
| public $line; | |
| /** | |
| * Load the string and split by line breaks. | |
| * Set delimiter and enclosing character. | |
| * @param string $string | |
| * @param string $delimiter | |
| * @param string $enclose | |
| * @return CsvReader | |
| */ | |
| static function load($string = null, $delimiter = ';', $enclose = '') | |
| { | |
| $object = new self; | |
| $object->delimiter = $delimiter; | |
| $object->enclose = $enclose; | |
| $object->stringToLines($string); | |
| return $object; | |
| } | |
| /** | |
| * Data from next single line. | |
| * @return array|false | |
| */ | |
| public function next() | |
| { | |
| $this->line = null === $this->line ? 1 : $this->line+1; | |
| return $this->lineToRow(); | |
| } | |
| /** | |
| * Data from previous single line. | |
| * @return array|false | |
| */ | |
| public function prev() | |
| { | |
| $this->line = (null||1) === $this->line ? 1 : $this->line-1; | |
| return $this->lineToRow(); | |
| } | |
| /** | |
| * Set the current $line to [n]. | |
| * @param int $offset | |
| */ | |
| public function offset($offset) | |
| { | |
| $this->line += $offset; | |
| } | |
| /** | |
| * Rewind back to first line. | |
| */ | |
| public function rewind() | |
| { | |
| $this->line = null; | |
| } | |
| /** | |
| * Split initial input by linebreak. Should work for Linux, Mac & Win. | |
| * @param string $string | |
| */ | |
| private function stringToLines($string) | |
| { | |
| $this->lines = preg_split('/[\r\n]{1,2}/', rtrim($string)); | |
| $this->lines = array_map('rtrim', $this->lines); | |
| } | |
| /** | |
| * Split the current line by delimiter and optional enclosing string. | |
| * e.g. ["some funny string";"another one";"a "cool" string"] should be fine | |
| * with $delimiter [;] and $enclose ["] | |
| * | |
| * @return array|false | |
| */ | |
| private function lineToRow() | |
| { | |
| if (isset($this->lines[$this->line-1])) | |
| { | |
| $line = substr( | |
| $this->lines[$this->line-1], | |
| strlen($this->enclose), | |
| strlen($this->lines[$this->line-1])-strlen($this->enclose)*2 | |
| ); | |
| $row = preg_split("/".$this->enclose.$this->delimiter.$this->enclose."/", $line); | |
| } | |
| else | |
| { | |
| $row = false; | |
| } | |
| return $row; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment