diff --git a/parsecsv.lib.php b/parsecsv.lib.php index f90c377..7d15f13 100644 --- a/parsecsv.lib.php +++ b/parsecsv.lib.php @@ -617,64 +617,73 @@ class parseCSV { * @param data CSV string * @return 2D array with CSV data, or false on failure */ - function parse_string ($data = null) { + public function parse_string ($data = null) { if ( empty($data) ) { if ( $this->_check_data() ) { $data = &$this->file_data; - } else return false; + } + else { + return false; + } } $white_spaces = str_replace($this->delimiter, '', " \t\x0B\0"); - $rows = array(); - $row = array(); - $row_count = 0; - $current = ''; - $head = ( !empty($this->fields) ) ? $this->fields : array() ; - $col = 0; - $enclosed = false; + $rows = array(); + $row = array(); + $row_count = 0; + $current = ''; + $head = ( !empty($this->fields) ) ? $this->fields : array() ; + $col = 0; + $enclosed = false; $was_enclosed = false; - $strlen = strlen($data); + $strlen = strlen($data); // walk through each character for ( $i=0; $i < $strlen; $i++ ) { - $ch = $data{$i}; + $ch = $data{$i}; $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) == '' ) { + if ( ltrim($current,$white_spaces) == '' ) { $enclosed = true; $was_enclosed = true; - } else { + } + else { $this->error = 2; - $error_row = count($rows) + 1; - $error_col = $col + 1; + $error_row = count($rows) + 1; + $error_col = $col + 1; 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.', - 'row' => $error_row, - 'field' => $error_col, + 'type' => 2, + 'info' => 'Syntax error found on row '.$error_row.'. Non-enclosed fields can not contain double-quotes.', + 'row' => $error_row, + 'field' => $error_col, 'field_name' => (!empty($head[$col])) ? $head[$col] : null, ); } + $current .= $ch; } - } elseif ($nch == $this->enclosure) { + } + elseif ($nch == $this->enclosure) { $current .= $ch; $i++; - } elseif ( $nch != $this->delimiter && $nch != "\r" && $nch != "\n" ) { - for ( $x=($i+1); isset($data{$x}) && ltrim($data{$x}, $white_spaces) == ''; $x++ ) {} + } + elseif ( $nch != $this->delimiter && $nch != "\r" && $nch != "\n" ) { + #for ( $x=($i+1); isset($data{$x}) && ltrim($data{$x}, $white_spaces) == ''; $x++ ) {} //this line does nothing? if ( $data{$x} == $this->delimiter ) { $enclosed = false; - $i = $x; - } else { + $i = $x; + } + else { 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]) ) { @@ -684,24 +693,27 @@ class parseCSV { 'Syntax error found on row '.(count($rows) + 1).'. '. 'A single double-quote was found within an enclosed string. '. 'Enclosed double-quotes must be escaped with a second double-quote.', - 'row' => count($rows) + 1, - 'field' => $col + 1, + 'row' => count($rows) + 1, + 'field' => $col + 1, 'field_name' => (!empty($head[$col])) ? $head[$col] : null, ); } + $current .= $ch; $enclosed = false; } - } else { + } + else { $enclosed = false; } // end of field/row - } elseif ( ($ch == $this->delimiter || $ch == "\n" || $ch == "\r") && !$enclosed ) { - $key = ( !empty($head[$col]) ) ? $head[$col] : $col ; - $row[$key] = ( $was_enclosed ) ? $current : trim($current) ; - $current = ''; - $was_enclosed = false; + } + elseif ( ($ch == $this->delimiter || $ch == "\n" || $ch == "\r") && !$enclosed ) { + $key = ( !empty($head[$col]) ) ? $head[$col] : $col ; + $row[$key] = ( $was_enclosed ) ? $current : trim($current) ; + $current = ''; + $was_enclosed = false; $col++; // end of row @@ -709,47 +721,64 @@ class parseCSV { 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)) ) { + } + 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++ ) {} //this line does nothing $rows[$row[$this->sort_by].'_'.$sn] = $row; - } else $rows[$row[$this->sort_by]] = $row; - } else $rows[] = $row; + } + else $rows[$row[$this->sort_by]] = $row; + } + else { + $rows[] = $row; + } } } + $row = array(); $col = 0; $row_count++; + if ( $this->sort_by === null && $this->limit !== null && count($rows) == $this->limit ) { $i = $strlen; } - if ( $ch == "\r" && $nch == "\n" ) $i++; + + if ( $ch == "\r" && $nch == "\n" ) { + $i++; + } } // append character to current field - } else { + } + else { $current .= $ch; } } + $this->titles = $head; if ( !empty($this->sort_by) ) { $sort_type = SORT_REGULAR; 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) ; + 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 ) { $this->file_data = null; } + return $rows; }