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