Force enclosing of all columns with optional switch

This commit is contained in:
Norcoen
2014-06-03 14:45:07 +02:00
parent 0486a4bf3e
commit 98a146bd38

View File

@@ -146,6 +146,15 @@ class parseCSV {
*/ */
public $enclosure = '"'; public $enclosure = '"';
/**
* Enclose All
* Force enclosing all columns
*
* @access public
* @var bool
*/
public $enclose_all = false;
/** /**
* Conditions * Conditions
* Basic SQL-Like conditions for row matching * Basic SQL-Like conditions for row matching
@@ -1049,7 +1058,7 @@ class parseCSV {
if ( $value !== null && $value != '' ) { if ( $value !== null && $value != '' ) {
$delimiter = preg_quote($this->delimiter, '/'); $delimiter = preg_quote($this->delimiter, '/');
$enclosure = preg_quote($this->enclosure, '/'); $enclosure = preg_quote($this->enclosure, '/');
if ( preg_match("/".$delimiter."|".$enclosure."|\n|\r/i", $value) || ($value{0} == ' ' || substr($value, -1) == ' ') ) { if ( preg_match("/".$delimiter."|".$enclosure."|\n|\r/i", $value) || ($value{0} == ' ' || substr($value, -1) == ' ') || $this->enclose_all ) {
$value = str_replace($this->enclosure, $this->enclosure.$this->enclosure, $value); $value = str_replace($this->enclosure, $this->enclosure.$this->enclosure, $value);
$value = $this->enclosure.$value.$this->enclosure; $value = $this->enclosure.$value.$this->enclosure;
} }