This commit is contained in:
William Knauss
2015-07-11 13:09:20 -04:00
parent a8b443d48c
commit ba4c30add1

View File

@@ -7,10 +7,10 @@ class parseCSV {
https://github.com/parsecsv/parsecsv-for-php https://github.com/parsecsv/parsecsv-for-php
Fully conforms to the specifications lined out on wikipedia: Fully conforms to the specifications lined out on wikipedia:
- http://en.wikipedia.org/wiki/Comma-separated_values - http://en.wikipedia.org/wiki/Comma-separated_values
Based on the concept of Ming Hong Ng's CsvFileParser class: Based on the concept of Ming Hong Ng's CsvFileParser class:
- http://minghong.blogspot.com/2006/07/csv-parser-for-php.html - http://minghong.blogspot.com/2006/07/csv-parser-for-php.html
(The MIT license) (The MIT license)
@@ -72,7 +72,7 @@ class parseCSV {
$csv = new parseCSV(); $csv = new parseCSV();
$csv->output('movies.csv', $array, array('field 1', 'field 2'), ','); $csv->output('movies.csv', $array, array('field 1', 'field 2'), ',');
---------------- ----------------
*/ */
/** /**
* Configuration * Configuration
@@ -339,7 +339,6 @@ class parseCSV {
*/ */
public $data = array(); public $data = array();
/** /**
* Constructor * Constructor
* Class constructor * Class constructor
@@ -350,7 +349,7 @@ class parseCSV {
* @param [integer] limit Limits the number of returned rows to specified amount * @param [integer] limit Limits the number of returned rows to specified amount
* @param [string] conditions Basic SQL-like conditions for row matching * @param [string] conditions Basic SQL-like conditions for row matching
*/ */
public function __construct ($input = null, $offset = null, $limit = null, $conditions = null, $keep_file_data = null) { public function __construct($input = null, $offset = null, $limit = null, $conditions = null, $keep_file_data = null) {
if (!is_null($offset)) { if (!is_null($offset)) {
$this->offset = $offset; $this->offset = $offset;
} }
@@ -364,7 +363,7 @@ class parseCSV {
} }
if (!is_null($keep_file_data)) { if (!is_null($keep_file_data)) {
$this->keep_file_data = $keep_file_data; $this->keep_file_data = $keep_file_data;
} }
if (!empty($input)) { if (!empty($input)) {
@@ -372,12 +371,10 @@ class parseCSV {
} }
} }
// ============================================== // ==============================================
// ----- [ Main Functions ] --------------------- // ----- [ Main Functions ] ---------------------
// ============================================== // ==============================================
/** /**
* Parse * Parse
* Parse a CSV file or string * Parse a CSV file or string
@@ -390,7 +387,7 @@ class parseCSV {
* *
* @return [bool] * @return [bool]
*/ */
public function parse ($input = null, $offset = null, $limit = null, $conditions = null) { public function parse($input = null, $offset = null, $limit = null, $conditions = null) {
if (is_null($input)) { if (is_null($input)) {
$input = $this->file; $input = $this->file;
} }
@@ -410,10 +407,9 @@ class parseCSV {
if (is_readable($input)) { if (is_readable($input)) {
$this->data = $this->parse_file($input); $this->data = $this->parse_file($input);
} } else {
else {
$this->file_data = &$input; $this->file_data = &$input;
$this->data = $this->parse_string(); $this->data = $this->parse_string();
} }
if ($this->data === false) { if ($this->data === false) {
@@ -436,12 +432,12 @@ class parseCSV {
* *
* @return [bool] * @return [bool]
*/ */
public function save ($file = null, $data = array(), $append = false, $fields = array()) { public function save($file = null, $data = array(), $append = false, $fields = array()) {
if (empty($file)) { if (empty($file)) {
$file = &$this->file; $file = &$this->file;
} }
$mode = ($append) ? 'at' : 'wt'; $mode = ($append) ? 'at' : 'wt';
$is_php = (preg_match('/\.php$/i', $file)) ? true : false; $is_php = (preg_match('/\.php$/i', $file)) ? true : false;
return $this->_wfile($file, $this->unparse($data, $fields, $append, $is_php), $mode); return $this->_wfile($file, $this->unparse($data, $fields, $append, $is_php), $mode);
@@ -459,7 +455,7 @@ class parseCSV {
* *
* @return [string] * @return [string]
*/ */
public function output ($filename = null, $data = array(), $fields = array(), $delimiter = null) { public function output($filename = null, $data = array(), $fields = array(), $delimiter = null) {
if (empty($filename)) { if (empty($filename)) {
$filename = $this->output_filename; $filename = $this->output_filename;
} }
@@ -472,11 +468,11 @@ class parseCSV {
if (!is_null($filename)) { if (!is_null($filename)) {
header('Content-type: application/csv'); header('Content-type: application/csv');
header('Content-Length: '.strlen($data)); header('Content-Length: ' . strlen($data));
header('Cache-Control: no-cache, must-revalidate'); header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache'); header('Pragma: no-cache');
header('Expires: 0'); header('Expires: 0');
header('Content-Disposition: attachment; filename="'.$filename.'"; modification-date="'.date('r').'";'); header('Content-Disposition: attachment; filename="' . $filename . '"; modification-date="' . date('r') . '";');
echo $data; echo $data;
} }
@@ -492,7 +488,7 @@ class parseCSV {
* @param [string] $input Input character encoding, uses default if left blank * @param [string] $input Input character encoding, uses default if left blank
* @param [string] $output Output character encoding, uses default if left blank * @param [string] $output Output character encoding, uses default if left blank
*/ */
public function encoding ($input = null, $output = null) { public function encoding($input = null, $output = null) {
$this->convert_encoding = true; $this->convert_encoding = true;
if (!is_null($input)) { if (!is_null($input)) {
$this->input_encoding = $input; $this->input_encoding = $input;
@@ -517,7 +513,7 @@ class parseCSV {
* *
* @return [string] * @return [string]
*/ */
public function auto ($file = null, $parse = true, $search_depth = null, $preferred = null, $enclosure = null) { public function auto($file = null, $parse = true, $search_depth = null, $preferred = null, $enclosure = null) {
if (is_null($file)) { if (is_null($file)) {
$file = $this->file; $file = $this->file;
} }
@@ -537,55 +533,48 @@ class parseCSV {
if (empty($this->file_data)) { if (empty($this->file_data)) {
if ($this->_check_data($file)) { if ($this->_check_data($file)) {
$data = &$this->file_data; $data = &$this->file_data;
} } else {
else {
return false; return false;
} }
} } else {
else {
$data = &$this->file_data; $data = &$this->file_data;
} }
$chars = array(); $chars = array();
$strlen = strlen($data); $strlen = strlen($data);
$enclosed = false; $enclosed = false;
$n = 1; $n = 1;
$to_end = true; $to_end = true;
// walk specific depth finding posssible delimiter characters // walk specific depth finding posssible delimiter characters
for ($i=0; $i < $strlen; $i++) { for ($i = 0; $i < $strlen; $i++) {
$ch = $data{$i}; $ch = $data{$i};
$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 and closing quotes // open and closing quotes
if ($ch == $enclosure) { if ($ch == $enclosure) {
if (!$enclosed || $nch != $enclosure) { if (!$enclosed || $nch != $enclosure) {
$enclosed = ($enclosed) ? false : true ; $enclosed = ($enclosed) ? false : true;
} } elseif ($enclosed) {
elseif ($enclosed) {
$i++; $i++;
} }
// end of row // end of row
} } elseif (($ch == "\n" && $pch != "\r" || $ch == "\r") && !$enclosed) {
elseif (($ch == "\n" && $pch != "\r" || $ch == "\r") && !$enclosed) {
if ($n >= $search_depth) { if ($n >= $search_depth) {
$strlen = 0; $strlen = 0;
$to_end = false; $to_end = false;
} } else {
else {
$n++; $n++;
} }
// count character // count character
} } elseif (!$enclosed) {
elseif (!$enclosed) { if (!preg_match('/[' . preg_quote($this->auto_non_chars, '/') . ']/i', $ch)) {
if (!preg_match('/['.preg_quote($this->auto_non_chars, '/').']/i', $ch)) {
if (!isset($chars[$ch][$n])) { if (!isset($chars[$ch][$n])) {
$chars[$ch][$n] = 1; $chars[$ch][$n] = 1;
} } else {
else {
$chars[$ch][$n]++; $chars[$ch][$n]++;
} }
} }
@@ -593,7 +582,7 @@ class parseCSV {
} }
// filtering // filtering
$depth = ($to_end) ? $n-1 : $n; $depth = ($to_end) ? $n - 1 : $n;
$filtered = array(); $filtered = array();
foreach ($chars as $char => $value) { foreach ($chars as $char => $value) {
if ($match = $this->_check_count($char, $value, $depth, $preferred)) { if ($match = $this->_check_count($char, $value, $depth, $preferred)) {
@@ -613,7 +602,6 @@ class parseCSV {
return $this->delimiter; return $this->delimiter;
} }
// ============================================== // ==============================================
// ----- [ Core Functions ] --------------------- // ----- [ Core Functions ] ---------------------
// ============================================== // ==============================================
@@ -628,7 +616,7 @@ class parseCSV {
* *
* @return [array|bool] * @return [array|bool]
*/ */
public function parse_file ($file = null) { public function parse_file($file = null) {
if (is_null($file)) { if (is_null($file)) {
$file = $this->file; $file = $this->file;
} }
@@ -648,91 +636,86 @@ 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 {
return false; return false;
} }
} }
$white_spaces = str_replace($this->delimiter, '', " \t\x0B\0"); $white_spaces = str_replace($this->delimiter, '', " \t\x0B\0");
$rows = array(); $rows = array();
$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;
$strlen = strlen($data); $strlen = strlen($data);
// force the parser to process end of data as a character (false) when // force the parser to process end of data as a character (false) when
// data does not end with a line feed or carriage return character. // data does not end with a line feed or carriage return character.
$lch = $data{$strlen-1}; $lch = $data{$strlen - 1};
if ($lch != "\n" && $lch != "\r") { if ($lch != "\n" && $lch != "\r") {
$strlen++; $strlen++;
} }
// 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.',
'row' => $error_row, 'row' => $error_row,
'field' => $error_col, 'field' => $error_col,
'field_name' => (!empty($head[$col])) ? $head[$col] : null, 'field_name' => (!empty($head[$col])) ? $head[$col] : null,
); );
} }
$current .= $ch; $current .= $ch;
} }
} } elseif ($nch == $this->enclosure) {
elseif ($nch == $this->enclosure) {
$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' =>
'Syntax error found on row '.(count($rows) + 1).'. '. 'Syntax error found on row ' . (count($rows) + 1) . '. ' .
'A single double-quote was found within an enclosed string. '. 'A single double-quote was found within an enclosed string. ' .
'Enclosed double-quotes must be escaped with a second double-quote.', 'Enclosed double-quotes must be escaped with a second double-quote.',
'row' => count($rows) + 1, 'row' => count($rows) + 1,
'field' => $col + 1, 'field' => $col + 1,
'field_name' => (!empty($head[$col])) ? $head[$col] : null, 'field_name' => (!empty($head[$col])) ? $head[$col] : null,
); );
} }
@@ -740,18 +723,16 @@ class parseCSV {
$current .= $ch; $current .= $ch;
$enclosed = false; $enclosed = false;
} }
} } else {
else {
$enclosed = false; $enclosed = false;
} }
// 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
@@ -759,18 +740,18 @@ class parseCSV {
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;
} } else {
else {
$rows[] = $row; $rows[] = $row;
} }
} }
@@ -789,9 +770,8 @@ class parseCSV {
} }
} }
// append character to current field // append character to current field
} } else {
else {
$current .= $ch; $current .= $ch;
} }
} }
@@ -801,15 +781,14 @@ class parseCSV {
$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);
} }
} }
@@ -833,12 +812,12 @@ class parseCSV {
* *
* @return CSV data (text string) * @return CSV data (text string)
*/ */
public function unparse ($data = array(), $fields = array(), $append = false , $is_php = false, $delimiter = null) { public function unparse($data = array(), $fields = array(), $append = false, $is_php = false, $delimiter = null) {
if (!is_array($data) || empty($data)) { if (!is_array($data) || empty($data)) {
$data = &$this->data; $data = &$this->data;
} }
if (!is_array($fields) || empty($fields)) { if (!is_array($fields) || empty($fields)) {
$fields = &$this->titles; $fields = &$this->titles;
} }
@@ -846,8 +825,8 @@ class parseCSV {
$delimiter = $this->delimiter; $delimiter = $this->delimiter;
} }
$string = ($is_php) ? "<?php header('Status: 403'); die(' '); ?>".$this->linefeed : ''; $string = ($is_php) ? "<?php header('Status: 403'); die(' '); ?>" . $this->linefeed : '';
$entry = array(); $entry = array();
// create heading // create heading
if ($this->heading && !$append && !empty($fields)) { if ($this->heading && !$append && !empty($fields)) {
@@ -855,8 +834,8 @@ class parseCSV {
$entry[] = $this->_enclose_value($value, $delimiter); $entry[] = $this->_enclose_value($value, $delimiter);
} }
$string .= implode($delimiter, $entry).$this->linefeed; $string .= implode($delimiter, $entry) . $this->linefeed;
$entry = array(); $entry = array();
} }
// create data // create data
@@ -865,8 +844,8 @@ class parseCSV {
$entry[] = $this->_enclose_value($value, $delimiter); $entry[] = $this->_enclose_value($value, $delimiter);
} }
$string .= implode($delimiter, $entry).$this->linefeed; $string .= implode($delimiter, $entry) . $this->linefeed;
$entry = array(); $entry = array();
} }
if ($this->convert_encoding) { if ($this->convert_encoding) {
@@ -884,17 +863,15 @@ class parseCSV {
* *
* @return true or false * @return true or false
*/ */
public function load_data ($input = null) { public function load_data($input = null) {
$data = null; $data = null;
$file = null; $file = null;
if (is_null($input)) { if (is_null($input)) {
$file = $this->file; $file = $this->file;
} } elseif (file_exists($input)) {
elseif (file_exists($input)) {
$file = $input; $file = $input;
} } else {
else {
$data = $input; $data = $input;
} }
@@ -922,7 +899,6 @@ class parseCSV {
return false; return false;
} }
// ============================================== // ==============================================
// ----- [ Internal Functions ] ----------------- // ----- [ Internal Functions ] -----------------
// ============================================== // ==============================================
@@ -936,7 +912,7 @@ class parseCSV {
* *
* @return true of false * @return true of false
*/ */
protected function _validate_row_conditions ($row = array(), $conditions = null) { protected function _validate_row_conditions($row = array(), $conditions = null) {
if (!empty($row)) { if (!empty($row)) {
if (!empty($conditions)) { if (!empty($conditions)) {
$conditions = (strpos($conditions, ' OR ') !== false) ? explode(' OR ', $conditions) : array($conditions); $conditions = (strpos($conditions, ' OR ') !== false) ? explode(' OR ', $conditions) : array($conditions);
@@ -944,15 +920,14 @@ class parseCSV {
foreach ($conditions as $key => $value) { foreach ($conditions as $key => $value) {
if (strpos($value, ' AND ') !== false) { if (strpos($value, ' AND ') !== false) {
$value = explode(' AND ', $value); $value = explode(' AND ', $value);
$and = ''; $and = '';
foreach ($value as $k => $v) { foreach ($value as $k => $v) {
$and .= $this->_validate_row_condition($row, $v); $and .= $this->_validate_row_condition($row, $v);
} }
$or .= (strpos($and, '0') !== false) ? '0' : '1'; $or .= (strpos($and, '0') !== false) ? '0' : '1';
} } else {
else {
$or .= $this->_validate_row_condition($row, $value); $or .= $this->_validate_row_condition($row, $value);
} }
} }
@@ -975,7 +950,7 @@ class parseCSV {
* *
* @return true of false * @return true of false
*/ */
protected function _validate_row_condition ($row, $condition) { protected function _validate_row_condition($row, $condition) {
$operators = array( $operators = array(
'=', 'equals', 'is', '=', 'equals', 'is',
'!=', 'is not', '!=', 'is not',
@@ -995,9 +970,9 @@ class parseCSV {
$operators_regex = implode('|', $operators_regex); $operators_regex = implode('|', $operators_regex);
if (preg_match('/^(.+) ('.$operators_regex.') (.+)$/i', trim($condition), $capture)) { if (preg_match('/^(.+) (' . $operators_regex . ') (.+)$/i', trim($condition), $capture)) {
$field = $capture[1]; $field = $capture[1];
$op = $capture[2]; $op = $capture[2];
$value = $capture[3]; $value = $capture[3];
if (preg_match('/^([\'\"]{1})(.*)([\'\"]{1})$/i', $value, $capture)) { if (preg_match('/^([\'\"]{1})(.*)([\'\"]{1})$/i', $value, $capture)) {
@@ -1013,29 +988,21 @@ class parseCSV {
if (array_key_exists($field, $row)) { if (array_key_exists($field, $row)) {
if (($op == '=' || $op == 'equals' || $op == 'is') && $row[$field] == $value) { if (($op == '=' || $op == 'equals' || $op == 'is') && $row[$field] == $value) {
return '1'; return '1';
} } elseif (($op == '!=' || $op == 'is not') && $row[$field] != $value) {
elseif (($op == '!=' || $op == 'is not') && $row[$field] != $value) {
return '1'; return '1';
} } elseif (($op == '<' || $op == 'is less than') && $row[$field] < $value) {
elseif (($op == '<' || $op == 'is less than' ) && $row[$field] < $value) {
return '1'; return '1';
} } elseif (($op == '>' || $op == 'is greater than') && $row[$field] > $value) {
elseif (($op == '>' || $op == 'is greater than') && $row[$field] > $value) {
return '1'; return '1';
} } elseif (($op == '<=' || $op == 'is less than or equals') && $row[$field] <= $value) {
elseif (($op == '<=' || $op == 'is less than or equals' ) && $row[$field] <= $value) {
return '1'; return '1';
} } elseif (($op == '>=' || $op == 'is greater than or equals') && $row[$field] >= $value) {
elseif (($op == '>=' || $op == 'is greater than or equals') && $row[$field] >= $value) {
return '1'; return '1';
} } elseif ($op == 'contains' && preg_match('/' . preg_quote($value, '/') . '/i', $row[$field])) {
elseif ($op == 'contains' && preg_match('/'.preg_quote($value, '/').'/i', $row[$field])) {
return '1'; return '1';
} } elseif ($op == 'does not contain' && !preg_match('/' . preg_quote($value, '/') . '/i', $row[$field])) {
elseif ($op == 'does not contain' && !preg_match('/'.preg_quote($value, '/').'/i', $row[$field])) {
return '1'; return '1';
} } else {
else {
return '0'; return '0';
} }
} }
@@ -1052,7 +1019,7 @@ class parseCSV {
* *
* @return true of false * @return true of false
*/ */
protected function _validate_offset ($current_row) { protected function _validate_offset($current_row) {
if ($this->sort_by === null && $this->offset !== null && $current_row < $this->offset) { if ($this->sort_by === null && $this->offset !== null && $current_row < $this->offset) {
return false; return false;
} }
@@ -1069,16 +1036,16 @@ class parseCSV {
* *
* @return Processed value * @return Processed value
*/ */
protected function _enclose_value ($value = null, $delimiter = null) { protected function _enclose_value($value = null, $delimiter = null) {
if (is_null($delimiter)) { if (is_null($delimiter)) {
$delimiter = $this->delimiter; $delimiter = $this->delimiter;
} }
if ($value !== null && $value != '') { if ($value !== null && $value != '') {
$delimiter_quoted = preg_quote($delimiter, '/'); $delimiter_quoted = preg_quote($delimiter, '/');
$enclosure_quoted = preg_quote($this->enclosure, '/'); $enclosure_quoted = preg_quote($this->enclosure, '/');
if (preg_match("/".$delimiter_quoted."|".$enclosure_quoted."|\n|\r/i", $value) || ($value{0} == ' ' || substr($value, -1) == ' ') || $this->enclose_all) { if (preg_match("/" . $delimiter_quoted . "|" . $enclosure_quoted . "|\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;
} }
} }
@@ -1093,9 +1060,11 @@ class parseCSV {
* *
* @return true or false * @return true or false
*/ */
protected function _check_data ($file = null) { protected function _check_data($file = null) {
if (empty($this->file_data)) { if (empty($this->file_data)) {
if (is_null($file)) $file = $this->file; if (is_null($file)) {
$file = $this->file;
}
return $this->load_data($file); return $this->load_data($file);
} }
@@ -1115,35 +1084,31 @@ class parseCSV {
* *
* @return special string used for delimiter selection, or false * @return special string used for delimiter selection, or false
*/ */
protected function _check_count ($char, $array, $depth, $preferred) { protected function _check_count($char, $array, $depth, $preferred) {
if ($depth == count($array)) { if ($depth == count($array)) {
$first = null; $first = null;
$equal = null; $equal = null;
$almost = false; $almost = false;
foreach ($array as $key => $value) { foreach ($array as $key => $value) {
if ($first == null) { if ($first == null) {
$first = $value; $first = $value;
} } elseif ($value == $first && $equal !== false) {
elseif ($value == $first && $equal !== false) { $equal = true;
} elseif ($value == $first + 1 && $equal !== false) {
$equal = true; $equal = true;
}
elseif ($value == $first+1 && $equal !== false) {
$equal = true;
$almost = true; $almost = true;
} } else {
else {
$equal = false; $equal = false;
} }
} }
if ($equal) { if ($equal) {
$match = ($almost) ? 2 : 1; $match = ($almost) ? 2 : 1;
$pref = strpos($preferred, $char); $pref = strpos($preferred, $char);
$pref = ($pref !== false) ? str_pad($pref, 3, '0', STR_PAD_LEFT) : '999'; $pref = ($pref !== false) ? str_pad($pref, 3, '0', STR_PAD_LEFT) : '999';
return $pref.$match.'.'.(99999 - str_pad($first, 5, '0', STR_PAD_LEFT)); return $pref . $match . '.' . (99999 - str_pad($first, 5, '0', STR_PAD_LEFT));
} } else {
else {
return false; return false;
} }
} }
@@ -1157,7 +1122,7 @@ class parseCSV {
* *
* @return Data from file, or false on failure * @return Data from file, or false on failure
*/ */
protected function _rfile ($file = null) { protected function _rfile($file = null) {
if (is_readable($file)) { if (is_readable($file)) {
if (!($fh = fopen($file, 'r'))) { if (!($fh = fopen($file, 'r'))) {
return false; return false;
@@ -1182,8 +1147,8 @@ class parseCSV {
* *
* @return true or false * @return true or false
*/ */
protected function _wfile ($file, $string = '') { protected function _wfile($file, $string = '') {
file_put_contents($file,$string); file_put_contents($file, $string);
return true; return true;
} }