sohotcall

Siesvi -- PHP CSV Parser

Mar 1st, 2021 (edited)
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.93 KB | None | 0 0
  1. <?php
  2. class Siesvi implements Iterator {
  3.     const FETCH_NUM = 1; // fetch NUM instead of ASSOC (auto ON if NOT_HAVE_HEADER)
  4.     const FETCH_OBJECT = 2; // fetch OBJECT instead of ARRAY
  5.     const NOT_HAVE_HEADER = 4; // first row is DATA instead of HEADER
  6.     public $fname;
  7.     public $mode;
  8.     // mode = 0; fetch array assoc, first row on the file is header
  9.     // mode = 1; fetch array num, first row on the file is header
  10.     // mode = 2; fetch object assoc, first row on the file is header
  11.     // mode = 3; fetch object num, first row on the file is header
  12.     // mode = 4,5; fetch array num, first row on the file is data
  13.     // mode = 6,7; fetch object num, first row on the file is data
  14.     private $fh;
  15.     public $pos;
  16.     public $row;
  17.     public $headers;
  18.     public function __construct( $fname, $mode = 0 ) {
  19.         $this->fname = $fname;
  20.         $this->mode = $mode;
  21.     }
  22.     public function rewind() {
  23.         if ( $this->fh )
  24.             fclose( $this->fh );
  25.         $this->fh = fopen( $this->fname, 'r' );
  26.         $this->headers = array();
  27.         if ( !( $this->mode & Siesvi::NOT_HAVE_HEADER ) ){
  28.             $this->next();
  29.             if ( $this->valid() )
  30.                 $this->headers = $this->row;
  31.         }
  32.         $this->pos = -1;
  33.         $this->next();
  34.     }
  35.     public function key() {
  36.         return $this->pos;
  37.     }
  38.     public function current() {
  39.         if ( $this->mode & Siesvi::FETCH_NUM || ! $this->headers ){
  40.             if ( $this->mode & Siesvi::FETCH_OBJECT )
  41.                 return (object) $this->row;
  42.             return $this->row;
  43.         } else {
  44.             if ( $this->mode & Siesvi::FETCH_OBJECT )
  45.                 return (object) array_combine( $this->headers, $this->row );
  46.             return array_combine( $this->headers, $this->row );
  47.         }
  48.     }
  49.     public function next() {
  50.         ++$this->pos;
  51.         $str = fgets( $this->fh );
  52.         if ( $str === false ){
  53.             fclose( $this->fh );
  54.             $this->fh = false;
  55.             return;
  56.         }
  57.         $quote = 0;
  58.         $cell = "";
  59.         $this->row = array();
  60.         for ( $i = 0, $strlen = strlen( $str ); $i < $strlen; $i++ ){
  61.             $chr = $str[$i];
  62.             if ( $quote == 0 && $chr == "," ){
  63.                 $this->row[] = $cell;
  64.                 $cell = "";
  65.             } elseif ( $quote == 0 && $chr == '"' ){
  66.                 $quote = 1;
  67.             } elseif ( $quote == 1 && $chr == '"' ){
  68.                 if ( $str[ $i + 1 ] == '"' )
  69.                     $cell .= $chr;
  70.                 $quote = 0;
  71.             } elseif ( $quote == 1 && $chr == "\n" ){
  72.                 $cell .= $chr;
  73.                 $str .= fgets( $this->fh );
  74.                 $strlen = strlen( $str );
  75.             } elseif ( $quote == 1 || ( $chr != "\r" && $chr != "\n" )) {
  76.                 $cell .= $chr;
  77.             }
  78.         }
  79.         $this->row[] = $cell;
  80.     }
  81.     public function valid() {
  82.         return $this->fh;
  83.     }
  84. }
  85. /*
  86. file_put_contents( __DIR__ . "/test.csv", <<<'EOD'
  87. id,"name",price,"notes"
  88. 1,"kucing ""kampung""",1000,"""Suaranya"" ""meong""
  89. ""meong"""
  90. 2,ayam", "dilepeh"""",2000,"Suaranya sreng-sreng, tadinya, pas digoreng.."
  91. EOD
  92. );
  93.  
  94.  
  95. $csv = new Siesvi( __DIR__ . "/test.csv" );
  96. foreach ( range(0,7) as $mode ){
  97.     $csv->mode = $mode;
  98.     echo sprintf( "MODE = %s\r\n", $mode );
  99.     foreach( $csv as $k => $v) {
  100.         echo sprintf( "Data ke-%s\r\n", $k );
  101.         print_r( $v );
  102.         echo "\r\n";
  103.     }
  104.     echo "----\r\n";
  105. }
  106. */
  107.  
Add Comment
Please, Sign In to add comment