CSV import

Ako imate peh da morate raditi redovan import podataka iz jedne baze, tko zna kako posložene u svoju (urednu!) bazu, ovakva skripta je korisna. Pronašao sam je na http://www.phpclasses.org/ i uredno obavlja svoj posao. Ova klasa podešena je da prebacuje podatke u neku bazu, a možete koristiti podatke i na neki drugačiji način, ispis na ekran ili sl.

Inicijalizacija objekta može biti na slijedeći način:

 

<?php
 
 
include("class_csvimport.php");

 
$csv =& new read_csv();
 
$csv->read_csv_run("foo.csv");
?>

Klasa u koju je smješten cijeli proces, nazvali smo class_csvimport.php izgleda ovako:

<?php
 
class read_csv {
    function
read_csv () {
       
// nothing
   
}

    function
read_csv_run($f="") {
   
// ako koristimo csv za upisivanje podatak u bazu, pozivamo connection string iz vanjske datoteke dbase.php
       
include("../dbase.php");

        if (
$f AND is_file($f) ) {
           
// set excel type delimiter, etc
           
$delimiter = ';';
           
$enclosure = '"';

           
// read file & parse
           
$input = file($f);
           
$csv = array();
            foreach (
$input as $key => $value ) {
               
// rtrim crap at the end of the str    ing
               
$tmp = explode($delimiter,rtrim($value));

               
// parse
               
$in_quote = false;
               
$arr = array();
                foreach (
$tmp as $key => $value ) {
                    if (
$in_quote ) {
                        if (
$this->read_csv_has_quote($value,$enclosure) ) {
                           
$in_quote = false;
                           
$value = substr_replace($value,'',-1,1);
                        }
                       
$key = (count($arr)-1);
                       
$arr[$key] .= $delimiter.$value; // continue last array element
                   
} else {
                        if (
$this->read_csv_has_quote($value,$enclosure) ) {
                           
$in_quote = true;
                           
$value = substr_replace($value,'',0,1);
                        } else if (
substr($value,0,1) == $enclosure AND substr($value,-1,1) == $enclosure ) {
                           
// string is quoted, remove quotes
                           
$value = substr_replace($value,'',0,1); // start
                           
$value = substr_replace($value,'',-1,1); // end
                       
}
                       
$arr[] = $value; // append to array
                   
}              
                }

                foreach (
$arr as $key ) {
                   
$arr[$key] = str_replace($enclosure.$enclosure,$enclosure,$value);
                }

               
// append to array
               
$csv[] = $arr;
            }
// end foreach
          
           
for($i = 0; $i < count($csv); $i++) {
             
$value = $csv[$i];
                 
             
$sql = mysqli_query($db, "INSERT INTO table (col1, col2, col3) VALUES ( '$value[0]', '$value[1]', '$value[2]') ");                 
              }       
        }  
    }  
// end func
     
   
function read_csv_has_quote ($str="",$enc="") {
       
$c = substr_count($str,$enc);
        if (
stristr(($c/2),".") ) {
            return
true;
        }
    }
    
          
}
// end class
?>