parse_string

This commit is contained in:
William Knauss
2014-06-05 20:44:14 -04:00
parent 710f22eeb9
commit 4d07760933

View File

@@ -649,8 +649,8 @@ class parseCSV {
* @return 2D array with CSV data, or false on failure
*/
public function parse_string ($data = null) {
if ( empty($data) ) {
if ( $this->_check_data() ) {
if (empty($data)) {
if ($this->_check_data()) {
$data = &$this->file_data;
}
else {
@@ -664,7 +664,7 @@ class parseCSV {
$row = array();
$row_count = 0;
$current = '';
$head = ( !empty($this->fields) ) ? $this->fields : array() ;
$head = (!empty($this->fields)) ? $this->fields : array();
$col = 0;
$enclosed = false;
$was_enclosed = false;
@@ -678,23 +678,23 @@ class parseCSV {
}
// walk through each character
for ( $i=0; $i < $strlen; $i++ ) {
$ch = ( isset($data{$i}) ) ? $data{$i} : false ;
$nch = ( isset($data{$i+1}) ) ? $data{$i+1} : false ;
$pch = ( isset($data{$i-1}) ) ? $data{$i-1} : false ;
for ($i=0; $i < $strlen; $i++) {
$ch = (isset($data{$i})) ? $data{$i} : false;
$nch = (isset($data{$i+1})) ? $data{$i+1} : false;
$pch = (isset($data{$i-1})) ? $data{$i-1} : false;
// open/close quotes, and inline quotes
if ( $ch == $this->enclosure ) {
if ( !$enclosed ) {
if ( ltrim($current,$white_spaces) == '' ) {
$enclosed = true;
if ($ch == $this->enclosure) {
if (!$enclosed) {
if (ltrim($current,$white_spaces) == '') {
$enclosed = true;
$was_enclosed = true;
}
else {
$this->error = 2;
$error_row = count($rows) + 1;
$error_col = $col + 1;
if ( !isset($this->error_info[$error_row.'-'.$error_col]) ) {
if (!isset($this->error_info[$error_row.'-'.$error_col])) {
$this->error_info[$error_row.'-'.$error_col] = array(
'type' => 2,
'info' => 'Syntax error found on row '.$error_row.'. Non-enclosed fields can not contain double-quotes.',
@@ -711,20 +711,20 @@ class parseCSV {
$current .= $ch;
$i++;
}
elseif ( $nch != $this->delimiter && $nch != "\r" && $nch != "\n" ) {
for ( $x=($i+1); isset($data{$x}) && ltrim($data{$x}, $white_spaces) == ''; $x++ ) {}
if ( $data{$x} == $this->delimiter ) {
elseif ($nch != $this->delimiter && $nch != "\r" && $nch != "\n") {
for ($x=($i+1); isset($data{$x}) && ltrim($data{$x}, $white_spaces) == ''; $x++) {}
if ($data{$x} == $this->delimiter) {
$enclosed = false;
$i = $x;
}
else {
if ( $this->error < 1 ) {
if ($this->error < 1) {
$this->error = 1;
}
$error_row = count($rows) + 1;
$error_col = $col + 1;
if ( !isset($this->error_info[$error_row.'-'.$error_col]) ) {
if (!isset($this->error_info[$error_row.'-'.$error_col])) {
$this->error_info[$error_row.'-'.$error_col] = array(
'type' => 1,
'info' =>
@@ -748,24 +748,24 @@ class parseCSV {
// end of field/row/csv
}
elseif ( ($ch == $this->delimiter || $ch == "\n" || $ch == "\r" || $ch === false) && !$enclosed ) {
$key = ( !empty($head[$col]) ) ? $head[$col] : $col ;
$row[$key] = ( $was_enclosed ) ? $current : trim($current) ;
$key = (!empty($head[$col])) ? $head[$col] : $col;
$row[$key] = ($was_enclosed) ? $current : trim($current);
$current = '';
$was_enclosed = false;
$col++;
// end of row
if ( $ch == "\n" || $ch == "\r" || $ch === false ) {
if ( $this->_validate_offset($row_count) && $this->_validate_row_conditions($row, $this->conditions) ) {
if ( $this->heading && empty($head) ) {
if ($ch == "\n" || $ch == "\r" || $ch === false) {
if ($this->_validate_offset($row_count) && $this->_validate_row_conditions($row, $this->conditions)) {
if ($this->heading && empty($head)) {
$head = $row;
}
elseif ( empty($this->fields) || (!empty($this->fields) && (($this->heading && $row_count > 0) || !$this->heading)) ) {
if ( !empty($this->sort_by) && !empty($row[$this->sort_by]) ) {
if ( isset($rows[$row[$this->sort_by]]) ) {
elseif (empty($this->fields) || (!empty($this->fields) && (($this->heading && $row_count > 0) || !$this->heading))) {
if (!empty($this->sort_by) && !empty($row[$this->sort_by])) {
if (isset($rows[$row[$this->sort_by]])) {
$rows[$row[$this->sort_by].'_0'] = &$rows[$row[$this->sort_by]];
unset($rows[$row[$this->sort_by]]);
for ( $sn=1; isset($rows[$row[$this->sort_by].'_'.$sn]); $sn++ ) {}
for ($sn=1; isset($rows[$row[$this->sort_by].'_'.$sn]); $sn++) {}
$rows[$row[$this->sort_by].'_'.$sn] = $row;
}
else $rows[$row[$this->sort_by]] = $row;
@@ -780,11 +780,11 @@ class parseCSV {
$col = 0;
$row_count++;
if ( $this->sort_by === null && $this->limit !== null && count($rows) == $this->limit ) {
if ($this->sort_by === null && $this->limit !== null && count($rows) == $this->limit) {
$i = $strlen;
}
if ( $ch == "\r" && $nch == "\n" ) {
if ($ch == "\r" && $nch == "\n") {
$i++;
}
}
@@ -797,23 +797,23 @@ class parseCSV {
}
$this->titles = $head;
if ( !empty($this->sort_by) ) {
if (!empty($this->sort_by)) {
$sort_type = SORT_REGULAR;
if ( $this->sort_type == 'numeric' ) {
if ($this->sort_type == 'numeric') {
$sort_type = SORT_NUMERIC;
}
elseif ( $this->sort_type == 'string' ) {
elseif ($this->sort_type == 'string') {
$sort_type = SORT_STRING;
}
( $this->sort_reverse ) ? krsort($rows, $sort_type) : ksort($rows, $sort_type) ;
($this->sort_reverse) ? krsort($rows, $sort_type) : ksort($rows, $sort_type);
if ( $this->offset !== null || $this->limit !== null ) {
if ($this->offset !== null || $this->limit !== null) {
$rows = array_slice($rows, ($this->offset === null ? 0 : $this->offset) , $this->limit, true);
}
}
if ( !$this->keep_file_data ) {
if (!$this->keep_file_data) {
$this->file_data = null;
}