<?php
/*
csv parser
//creating object:
$csv = new parseCSVtoArray();
//CONFIGURATION
//prepare config:
$config = array(
'SEPARATOR' => ",", //separator, default ,
'NEWLINE' => "\n", //new line, default \n (all \r are converted to \n)
'ENCLOSE' => '"', //enclose mark, default " (double qoute mark)
'TRIM' => false, //trim text? true|false, default false
'SKIPLASTEMPTY' => false //Skip last row if empty? true|false, default false;
);
//append config:
$csv -> set($cfg);
//parse text
$csv->parseFromText('1,2,3');
//or file
$csv->parseFromFile('file.csv');
//print_r file
echo '<pre>' . print_r('file.csv', true) .'</pre>';
*/
class parseCSVtoArray{
var $sep = ',';
var $nl = "\n";
var $enc = '"';
var $trim = false;
var $skipLE = false;
var $err = array();
function parseCSVtoArray($arr = array()){$this->set($arr);}
function error($e){$this->err[] = $e;}
function set($arr = array()){
if(array_key_exists('SEPARATOR', $arr)){
if(strlen($arr['SEPARATOR'])==1){
$this->sep = $arr['SEPARATOR'];
}
}
if(array_key_exists('NEWLINE', $arr)){
if(strlen($arr['NEWLINE'])==1){
$this->nl = $arr['NEWLINE'];
}
}
if(array_key_exists('ENCLOSE', $arr)){
if(strlen($arr['ENCLOSE'])==1){
$this->enc = $arr['ENCLOSE'];
}
}
if(array_key_exists('TRIM', $arr)){
if($arr['TRIM'] === false || $arr['TRIM'] === true){
$this->trim = $arr['TRIM'];
}
}
if(array_key_exists('SKIPLASTEMPTY', $arr)){
if($arr['SKIPLASTEMPTY'] === false || $arr['SKIPLASTEMPTY'] === true){
$this->skipLE = $arr['SKIPLASTEMPTY'];
}
}
}
function parseFromFile($file){
if($txt = file_get_contents($file)){
return $this->parseFromText($txt);
}else{
$this->error('cannot load file ' . htmlspecialchars($file));
}
}
function parseFromText($txt){
$txt = str_replace(array("\r\n", "\r"), "\n", $txt);
if($this->trim){$txt = trim($txt);}//trim if neccessary
$en = false; //czy sprawdza właśnie w uszkach
$ttxt = false;
$row = array();
$csv = array();
$i = 0;
//parsuj plik znak po znaku
while ($i<strlen($txt)){
//jeśli początek pliku lub nowa linia
//tekst nie będzie pusty (gdy będzie separator jako pierwszy)
if($i === 0 || $txt[$i-1] == $this->nl){
$ttxt='';
}
//parsuj w zależności czy w uszkach (true) czy nie (false)
if($en == false){
if($txt[$i] == $this->enc){
$i++;
$en = true;
}elseif($txt[$i] == $this->sep){
$en = false;
if($ttxt !== false){
$row[]=$ttxt;
$ttxt = false;
}
//sprawdź jaki jest kolejny znak i ew. ustaw tekst na pusty nie false;
if($txt[$i+1] == $this->sep || $i+1 == strlen($txt) || $txt[$i+1] == $this->nl){
$ttxt .= '';
}
$i++;
}elseif($txt[$i] == $this->nl){
$en = false;
if($ttxt !== false){
$row[]=$ttxt;
$ttxt = false;
}
if(count($row)!==0){
$csv[] = $row;
}
$row = array();
$i++;
}else{
$ttxt .= $txt[$i];
$i++;
}
}else{
if($txt[$i] == $this->enc){
if($txt[$i+1] . $txt[$i+2] == $this->enc . $this->sep){
$en = false;
$row[]='';
$ttxt = false;
}elseif($txt[$i+1]==$this->enc){
$ttxt .= $txt[$i];
$i++;
}else{
$en = false;
$row[]=$ttxt;
$ttxt = false;
}
$i++;
}else{
$ttxt .= $txt[$i];
$i++;
}
}
}
if($ttxt !== false){
$row[]=$ttxt;
}
if(count($row)!==0){
$csv[] = $row;
}else{
if(!$this->skipLE){
$csv[] = array('');
}
}
return $csv;
}
}
$csv = new parseCSVtoArray();
$cfg = array(
'SEPARATOR'=>';',
'SKIPLASTEMPTY'=>true
);
$csv -> set($cfg);
echo '<pre>';
print_r($csv->parseFromFile('dane dodaj.csv'));
echo '</pre>';
?>