Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Siesvi implements Iterator {
- const FETCH_NUM = 1; // fetch NUM instead of ASSOC (auto ON if NOT_HAVE_HEADER)
- const FETCH_OBJECT = 2; // fetch OBJECT instead of ARRAY
- const NOT_HAVE_HEADER = 4; // first row is DATA instead of HEADER
- public $fname;
- public $mode;
- // mode = 0; fetch array assoc, first row on the file is header
- // mode = 1; fetch array num, first row on the file is header
- // mode = 2; fetch object assoc, first row on the file is header
- // mode = 3; fetch object num, first row on the file is header
- // mode = 4,5; fetch array num, first row on the file is data
- // mode = 6,7; fetch object num, first row on the file is data
- private $fh;
- public $pos;
- public $row;
- public $headers;
- public function __construct( $fname, $mode = 0 ) {
- $this->fname = $fname;
- $this->mode = $mode;
- }
- public function rewind() {
- if ( $this->fh )
- fclose( $this->fh );
- $this->fh = fopen( $this->fname, 'r' );
- $this->headers = array();
- if ( !( $this->mode & Siesvi::NOT_HAVE_HEADER ) ){
- $this->next();
- if ( $this->valid() )
- $this->headers = $this->row;
- }
- $this->pos = -1;
- $this->next();
- }
- public function key() {
- return $this->pos;
- }
- public function current() {
- if ( $this->mode & Siesvi::FETCH_NUM || ! $this->headers ){
- if ( $this->mode & Siesvi::FETCH_OBJECT )
- return (object) $this->row;
- return $this->row;
- } else {
- if ( $this->mode & Siesvi::FETCH_OBJECT )
- return (object) array_combine( $this->headers, $this->row );
- return array_combine( $this->headers, $this->row );
- }
- }
- public function next() {
- ++$this->pos;
- $str = fgets( $this->fh );
- if ( $str === false ){
- fclose( $this->fh );
- $this->fh = false;
- return;
- }
- $quote = 0;
- $cell = "";
- $this->row = array();
- for ( $i = 0, $strlen = strlen( $str ); $i < $strlen; $i++ ){
- $chr = $str[$i];
- if ( $quote == 0 && $chr == "," ){
- $this->row[] = $cell;
- $cell = "";
- } elseif ( $quote == 0 && $chr == '"' ){
- $quote = 1;
- } elseif ( $quote == 1 && $chr == '"' ){
- if ( $str[ $i + 1 ] == '"' )
- $cell .= $chr;
- $quote = 0;
- } elseif ( $quote == 1 && $chr == "\n" ){
- $cell .= $chr;
- $str .= fgets( $this->fh );
- $strlen = strlen( $str );
- } elseif ( $quote == 1 || ( $chr != "\r" && $chr != "\n" )) {
- $cell .= $chr;
- }
- }
- $this->row[] = $cell;
- }
- public function valid() {
- return $this->fh;
- }
- }
- /*
- file_put_contents( __DIR__ . "/test.csv", <<<'EOD'
- id,"name",price,"notes"
- 1,"kucing ""kampung""",1000,"""Suaranya"" ""meong""
- ""meong"""
- 2,ayam", "dilepeh"""",2000,"Suaranya sreng-sreng, tadinya, pas digoreng.."
- EOD
- );
- $csv = new Siesvi( __DIR__ . "/test.csv" );
- foreach ( range(0,7) as $mode ){
- $csv->mode = $mode;
- echo sprintf( "MODE = %s\r\n", $mode );
- foreach( $csv as $k => $v) {
- echo sprintf( "Data ke-%s\r\n", $k );
- print_r( $v );
- echo "\r\n";
- }
- echo "----\r\n";
- }
- */
Add Comment
Please, Sign In to add comment