<?php
class classCsv
{
//private $to, $subject, $message, $headers;
private $parse, $patt;

function __construct()
	{
	$this->parse = array(
		'row' => "",
		'col' => '',
		'enc' => '',
		'esc' => ''
		);
	$parse = array(	// excel csv
		'row' => "\n",	// row separator
		'col' => ';',	// col separator
		'enc' => '"',	// enclosure string
		'esc' => '"'	// escape csv chars in string
		);
	$this->setOpt($parse);
	}

public function escapeRegExp($str)
	{
	return preg_quote($str);
	}

public function setOpt($parse)
	{
	$new_parse = $this->parse;
	foreach($new_parse as $key=>$value)
		{
		if (!isset($parse[$key]) || !is_string($parse[$key]))
			{
			continue;
			}
		elseif( strlen($parse[$key]) < 1 )
			{
			trigger_error($key.' must be a character', E_USER_WARNING);
			continue;
			}
		elseif( strlen($parse[$key]) > 1 )
			{
			$new_parse[$key] = $parse[$key][0];
			trigger_error($key.' must be a single character', E_USER_NOTICE);
			continue;
			}
		$new_parse[$key] = $parse[$key];
		}
	$this->parse = $new_parse;
//var_dump($new_parse);
	$p = array();
	foreach ($new_parse as $key=>$value)
		{
		$p[$key] = $this->escapeRegExp($value);
		}
	list($row, $col, $enc, $esc) = array($p['row'], $p['col'], $p['enc'], $p['esc']);
//var_dump(array(
//$p['row'], $p['col'], $p['enc'], $p['esc'],
//$row, $col, $enc, $esc
//));
	$r = '\\r';
	$p['val_enc'] = '([' . $enc . '])';	// export 1
	$p['val_sep'] = '[' . $col . $row . ']';// export 2
	$p['imp1'] = $esc . $enc;		// import 1
	$p['imp2'] = "(" . $col . "|" . $r . "?" . $row . "|" . $r . "|^)" .		// Delimiters
		 "(?:" . $enc . "([^" . $enc . "]*(?:" . $p['imp1'] . "[^" . $enc . "]*)*)" . $enc . "|" .	// Quoted fields
		 "([^". $enc . $col  . $r . $row . "]*))";		// Standard fields
	$this->patt = $p;
	}

public function newRegExp($patt='', $mod='')
	{
	return '~' . preg_replace('~\~~', '\\~', $patt) . '~' . $mod;
	}

public function escapeValue($str='', $escape_always=false)
	{
	$csv  = $this->parse;
	$patt = $this->patt;
	$str  = $str . ''; //convert to string
	$re   = $this->newRegExp($patt['val_enc'], 's');
	$repl = $csv['esc'] . '$1';
//var_dump($re);
	$str  = preg_replace($re, $repl, $str);
	$re   = $this->newRegExp($patt['val_sep'], 's');
	$str  = $escape_always==true || preg_match($re, $str)!==false ? $csv['esc'] . $str . $csv['esc'] : $str;
	// ignore RFC 4180
	return $str;
	}



public function export($rows=array(), $escape_always=false)	// escapeRows
	{
	$escape_always = $escape_always ? true : false;
	$csv   = $this->parse;
	$out   = array();
	$i_end = count($rows);
	for ($i=0; $i<$i_end; ++$i)
		{
		$cols  = $rows[$i];
		$j_end = count($cols);
		for ($j=0; $j<$j_end; ++$j)
			{
			$cols[$j] = $this->escapeValue($cols[$j], $escape_always);
			}
		$out[$i] = implode($csv['col'], $cols);
		}
	return implode($csv['row'], $out);
	}

/*
this.import = function(strData,cb_escape)
	{
        var out, arrMatches, strMatchedDelimiter, strMatchedValue, re1, re2, bool;
	csv  = this.opt.parse;
	patt = this.data.patt;
	bool = isFunction(cb_escape);
        re1  = new RegExp(patt.imp2, "gi");
        out  = [[]];
        arrMatches = null;
        while (arrMatches = re1.exec( strData ))
		{
		strMatchedDelimiter = arrMatches[ 1 ];
		if (strMatchedDelimiter.length && strMatchedDelimiter !== csv.col)
			{
			out.push([]);
			}
		if (arrMatches[ 2 ])
			{
			re2 = new RegExp( patt.imp1, "g" );
			strMatchedValue = arrMatches[ 2 ].replace(re2, csv.enc);
			}
		else	{
			strMatchedValue = arrMatches[ 3 ];
			}
		strMatchedValue = typeof(strMatchedValue)!='undefined' ? strMatchedValue + '' : '';
		strMatchedValue = bool ? cb_escape(strMatchedValue) : strMatchedValue;
		out[out.length-1].push(strMatchedValue);
		}
	return out;
	}
*/
}

/*
this.export = function(arrData,escape_always)
	{
	var i, row, csv, out;
	var escape_always = !isExist(escape_always) ? false : escape_always;
	csv = this.opt.parse;
	out = [];
	for (i=0; i<arrData.length; ++i)
		{
		row = arrData[i];
		for (j=0; j<row.length; ++j)
			{
			row[j] = this.escapeCsvValue(row[j],escape_always);
			}
		out[i] = row.join(csv.col);
		}
	return out.join(csv.row);
	}

*/
?>