mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 08:36:39 +00:00
264
parsecsv.lib.php
264
parsecsv.lib.php
@@ -351,23 +351,23 @@ class parseCSV {
|
||||
* @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) {
|
||||
if ( $offset !== null ) {
|
||||
if (!is_null($offset)) {
|
||||
$this->offset = $offset;
|
||||
}
|
||||
|
||||
if ( $limit !== null ) {
|
||||
if (!is_null($limit)) {
|
||||
$this->limit = $limit;
|
||||
}
|
||||
|
||||
if ( !is_null($conditions) ) {
|
||||
if (!is_null($conditions)) {
|
||||
$this->conditions = $conditions;
|
||||
}
|
||||
|
||||
if ( !is_null($keep_file_data) ) {
|
||||
if (!is_null($keep_file_data)) {
|
||||
$this->keep_file_data = $keep_file_data;
|
||||
}
|
||||
|
||||
if ( !empty($input) ) {
|
||||
if (!empty($input)) {
|
||||
$this->parse($input);
|
||||
}
|
||||
}
|
||||
@@ -391,24 +391,24 @@ class parseCSV {
|
||||
* @return [bool]
|
||||
*/
|
||||
public function parse ($input = null, $offset = null, $limit = null, $conditions = null) {
|
||||
if ( $input === null ) {
|
||||
if (is_null($input)) {
|
||||
$input = $this->file;
|
||||
}
|
||||
|
||||
if ( !empty($input) ) {
|
||||
if ( $offset !== null ) {
|
||||
if (!empty($input)) {
|
||||
if (!is_null($offset)) {
|
||||
$this->offset = $offset;
|
||||
}
|
||||
|
||||
if ($limit !== null ) {
|
||||
if (!is_null($limit)) {
|
||||
$this->limit = $limit;
|
||||
}
|
||||
|
||||
if ( !is_null($conditions) ) {
|
||||
if (!is_null($conditions)) {
|
||||
$this->conditions = $conditions;
|
||||
}
|
||||
|
||||
if ( is_readable($input) ) {
|
||||
if (is_readable($input)) {
|
||||
$this->data = $this->parse_file($input);
|
||||
}
|
||||
else {
|
||||
@@ -416,7 +416,7 @@ class parseCSV {
|
||||
$this->data = $this->parse_string();
|
||||
}
|
||||
|
||||
if ( $this->data === false ) {
|
||||
if ($this->data === false) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -437,7 +437,7 @@ class parseCSV {
|
||||
* @return [bool]
|
||||
*/
|
||||
public function save ($file = null, $data = array(), $append = false, $fields = array()) {
|
||||
if ( empty($file) ) {
|
||||
if (empty($file)) {
|
||||
$file = &$this->file;
|
||||
}
|
||||
|
||||
@@ -460,17 +460,17 @@ class parseCSV {
|
||||
* @return [string]
|
||||
*/
|
||||
public function output ($filename = null, $data = array(), $fields = array(), $delimiter = null) {
|
||||
if ( empty($filename) ) {
|
||||
if (empty($filename)) {
|
||||
$filename = $this->output_filename;
|
||||
}
|
||||
|
||||
if ( $delimiter === null ) {
|
||||
if ($delimiter === null) {
|
||||
$delimiter = $this->output_delimiter;
|
||||
}
|
||||
|
||||
$data = $this->unparse($data, $fields, null, null, $delimiter);
|
||||
|
||||
if ( $filename !== null ) {
|
||||
if (!is_null($filename)) {
|
||||
header('Content-type: application/csv');
|
||||
header('Content-Length: '.strlen($data));
|
||||
header('Cache-Control: no-cache, must-revalidate');
|
||||
@@ -494,11 +494,11 @@ class parseCSV {
|
||||
*/
|
||||
public function encoding ($input = null, $output = null) {
|
||||
$this->convert_encoding = true;
|
||||
if ( $input !== null ) {
|
||||
if (!is_null($input)) {
|
||||
$this->input_encoding = $input;
|
||||
}
|
||||
|
||||
if ( $output !== null ) {
|
||||
if (!is_null($output)) {
|
||||
$this->output_encoding = $output;
|
||||
}
|
||||
}
|
||||
@@ -518,24 +518,24 @@ class parseCSV {
|
||||
* @return [string]
|
||||
*/
|
||||
public function auto ($file = null, $parse = true, $search_depth = null, $preferred = null, $enclosure = null) {
|
||||
if ( $file === null ) {
|
||||
if (is_null($file)) {
|
||||
$file = $this->file;
|
||||
}
|
||||
|
||||
if ( empty($search_depth) ) {
|
||||
if (empty($search_depth)) {
|
||||
$search_depth = $this->auto_depth;
|
||||
}
|
||||
|
||||
if ( $enclosure === null ) {
|
||||
if (is_null($enclosure)) {
|
||||
$enclosure = $this->enclosure;
|
||||
}
|
||||
|
||||
if ( $preferred === null ) {
|
||||
if (is_null($preferred)) {
|
||||
$preferred = $this->auto_preferred;
|
||||
}
|
||||
|
||||
if ( empty($this->file_data) ) {
|
||||
if ( $this->_check_data($file) ) {
|
||||
if (empty($this->file_data)) {
|
||||
if ($this->_check_data($file)) {
|
||||
$data = &$this->file_data;
|
||||
}
|
||||
else {
|
||||
@@ -553,24 +553,24 @@ class parseCSV {
|
||||
$to_end = true;
|
||||
|
||||
// walk specific depth finding posssible delimiter characters
|
||||
for ( $i=0; $i < $strlen; $i++ ) {
|
||||
for ($i=0; $i < $strlen; $i++) {
|
||||
$ch = $data{$i};
|
||||
$nch = ( isset($data{$i+1}) ) ? $data{$i+1} : false ;
|
||||
$pch = ( 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 ;
|
||||
|
||||
// open and closing quotes
|
||||
if ( $ch == $enclosure ) {
|
||||
if ( !$enclosed || $nch != $enclosure ) {
|
||||
$enclosed = ( $enclosed ) ? false : true ;
|
||||
if ($ch == $enclosure) {
|
||||
if (!$enclosed || $nch != $enclosure) {
|
||||
$enclosed = ($enclosed) ? false : true ;
|
||||
}
|
||||
elseif ( $enclosed ) {
|
||||
elseif ($enclosed) {
|
||||
$i++;
|
||||
}
|
||||
|
||||
// end of row
|
||||
}
|
||||
elseif ( ($ch == "\n" && $pch != "\r" || $ch == "\r") && !$enclosed ) {
|
||||
if ( $n >= $search_depth ) {
|
||||
elseif (($ch == "\n" && $pch != "\r" || $ch == "\r") && !$enclosed) {
|
||||
if ($n >= $search_depth) {
|
||||
$strlen = 0;
|
||||
$to_end = false;
|
||||
}
|
||||
@@ -581,8 +581,8 @@ class parseCSV {
|
||||
// count character
|
||||
}
|
||||
elseif (!$enclosed) {
|
||||
if ( !preg_match('/['.preg_quote($this->auto_non_chars, '/').']/i', $ch) ) {
|
||||
if ( !isset($chars[$ch][$n]) ) {
|
||||
if (!preg_match('/['.preg_quote($this->auto_non_chars, '/').']/i', $ch)) {
|
||||
if (!isset($chars[$ch][$n])) {
|
||||
$chars[$ch][$n] = 1;
|
||||
}
|
||||
else {
|
||||
@@ -593,10 +593,10 @@ class parseCSV {
|
||||
}
|
||||
|
||||
// filtering
|
||||
$depth = ( $to_end ) ? $n-1 : $n ;
|
||||
$depth = ($to_end) ? $n-1 : $n;
|
||||
$filtered = array();
|
||||
foreach( $chars as $char => $value ) {
|
||||
if ( $match = $this->_check_count($char, $value, $depth, $preferred) ) {
|
||||
foreach ($chars as $char => $value) {
|
||||
if ($match = $this->_check_count($char, $value, $depth, $preferred)) {
|
||||
$filtered[$match] = $char;
|
||||
}
|
||||
}
|
||||
@@ -606,7 +606,7 @@ class parseCSV {
|
||||
$this->delimiter = reset($filtered);
|
||||
|
||||
// parse data
|
||||
if ( $parse ) {
|
||||
if ($parse) {
|
||||
$this->data = $this->parse_string();
|
||||
}
|
||||
|
||||
@@ -629,15 +629,15 @@ class parseCSV {
|
||||
* @return [array|bool]
|
||||
*/
|
||||
public function parse_file ($file = null) {
|
||||
if ( $file === null ) {
|
||||
if (is_null($file)) {
|
||||
$file = $this->file;
|
||||
}
|
||||
|
||||
if ( empty($this->file_data) ) {
|
||||
if (empty($this->file_data)) {
|
||||
$this->load_data($file);
|
||||
}
|
||||
|
||||
return ( !empty($this->file_data) ) ? $this->parse_string() : false ;
|
||||
return (!empty($this->file_data)) ? $this->parse_string() : false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -833,25 +833,25 @@ class parseCSV {
|
||||
*
|
||||
* @return CSV data (text string)
|
||||
*/
|
||||
public function unparse ( $data = array(), $fields = array(), $append = false , $is_php = false, $delimiter = null) {
|
||||
if ( !is_array($data) || empty($data) ) {
|
||||
public function unparse ($data = array(), $fields = array(), $append = false , $is_php = false, $delimiter = null) {
|
||||
if (!is_array($data) || empty($data)) {
|
||||
$data = &$this->data;
|
||||
}
|
||||
|
||||
if ( !is_array($fields) || empty($fields) ) {
|
||||
if (!is_array($fields) || empty($fields)) {
|
||||
$fields = &$this->titles;
|
||||
}
|
||||
|
||||
if ( $delimiter === null ) {
|
||||
if ($delimiter === null) {
|
||||
$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();
|
||||
|
||||
// create heading
|
||||
if ( $this->heading && !$append && !empty($fields) ) {
|
||||
foreach( $fields as $key => $value ) {
|
||||
if ($this->heading && !$append && !empty($fields)) {
|
||||
foreach ($fields as $key => $value) {
|
||||
$entry[] = $this->_enclose_value($value, $delimiter);
|
||||
}
|
||||
|
||||
@@ -860,8 +860,8 @@ class parseCSV {
|
||||
}
|
||||
|
||||
// create data
|
||||
foreach( $data as $key => $row ) {
|
||||
foreach( $row as $field => $value ) {
|
||||
foreach ($data as $key => $row) {
|
||||
foreach ($row as $field => $value) {
|
||||
$entry[] = $this->_enclose_value($value, $delimiter);
|
||||
}
|
||||
|
||||
@@ -884,30 +884,30 @@ class parseCSV {
|
||||
$data = null;
|
||||
$file = null;
|
||||
|
||||
if ( $input === null ) {
|
||||
if (is_null($input)) {
|
||||
$file = $this->file;
|
||||
}
|
||||
elseif ( file_exists($input) ) {
|
||||
elseif (file_exists($input)) {
|
||||
$file = $input;
|
||||
}
|
||||
else {
|
||||
$data = $input;
|
||||
}
|
||||
|
||||
if ( !empty($data) || $data = $this->_rfile($file) ) {
|
||||
if ( $this->file != $file ) {
|
||||
if (!empty($data) || $data = $this->_rfile($file)) {
|
||||
if ($this->file != $file) {
|
||||
$this->file = $file;
|
||||
}
|
||||
|
||||
if ( preg_match('/\.php$/i', $file) && preg_match('/<\?.*?\?>(.*)/ims', $data, $strip) ) {
|
||||
if (preg_match('/\.php$/i', $file) && preg_match('/<\?.*?\?>(.*)/ims', $data, $strip)) {
|
||||
$data = ltrim($strip[1]);
|
||||
}
|
||||
|
||||
if ( $this->convert_encoding ) {
|
||||
if ($this->convert_encoding) {
|
||||
$data = iconv($this->input_encoding, $this->output_encoding, $data);
|
||||
}
|
||||
|
||||
if ( substr($data, -1) != "\n" ) {
|
||||
if (substr($data, -1) != "\n") {
|
||||
$data .= "\n";
|
||||
}
|
||||
|
||||
@@ -933,27 +933,27 @@ class parseCSV {
|
||||
* @return true of false
|
||||
*/
|
||||
protected function _validate_row_conditions ($row = array(), $conditions = null) {
|
||||
if ( !empty($row) ) {
|
||||
if ( !empty($conditions) ) {
|
||||
$conditions = (strpos($conditions, ' OR ') !== false) ? explode(' OR ', $conditions) : array($conditions) ;
|
||||
if (!empty($row)) {
|
||||
if (!empty($conditions)) {
|
||||
$conditions = (strpos($conditions, ' OR ') !== false) ? explode(' OR ', $conditions) : array($conditions);
|
||||
$or = '';
|
||||
foreach( $conditions as $key => $value ) {
|
||||
if ( strpos($value, ' AND ') !== false ) {
|
||||
foreach ($conditions as $key => $value) {
|
||||
if (strpos($value, ' AND ') !== false) {
|
||||
$value = explode(' AND ', $value);
|
||||
$and = '';
|
||||
|
||||
foreach( $value as $k => $v ) {
|
||||
foreach ($value as $k => $v) {
|
||||
$and .= $this->_validate_row_condition($row, $v);
|
||||
}
|
||||
|
||||
$or .= (strpos($and, '0') !== false) ? '0' : '1' ;
|
||||
$or .= (strpos($and, '0') !== false) ? '0' : '1';
|
||||
}
|
||||
else {
|
||||
$or .= $this->_validate_row_condition($row, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return (strpos($or, '1') !== false) ? true : false ;
|
||||
return (strpos($or, '1') !== false) ? true : false;
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -985,19 +985,19 @@ class parseCSV {
|
||||
|
||||
$operators_regex = array();
|
||||
|
||||
foreach( $operators as $value ) {
|
||||
foreach ($operators as $value) {
|
||||
$operators_regex[] = preg_quote($value, '/');
|
||||
}
|
||||
|
||||
$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];
|
||||
$op = $capture[2];
|
||||
$value = $capture[3];
|
||||
|
||||
if ( preg_match('/^([\'\"]{1})(.*)([\'\"]{1})$/i', $value, $capture) ) {
|
||||
if ( $capture[1] == $capture[3] ) {
|
||||
if (preg_match('/^([\'\"]{1})(.*)([\'\"]{1})$/i', $value, $capture)) {
|
||||
if ($capture[1] == $capture[3]) {
|
||||
$value = $capture[2];
|
||||
$value = str_replace("\\n", "\n", $value);
|
||||
$value = str_replace("\\r", "\r", $value);
|
||||
@@ -1006,29 +1006,29 @@ class parseCSV {
|
||||
}
|
||||
}
|
||||
|
||||
if ( array_key_exists($field, $row) ) {
|
||||
if ( ($op == '=' || $op == 'equals' || $op == 'is') && $row[$field] == $value ) {
|
||||
if (array_key_exists($field, $row)) {
|
||||
if (($op == '=' || $op == 'equals' || $op == 'is') && $row[$field] == $value) {
|
||||
return '1';
|
||||
}
|
||||
elseif ( ($op == '!=' || $op == 'is not') && $row[$field] != $value ) {
|
||||
elseif (($op == '!=' || $op == 'is not') && $row[$field] != $value) {
|
||||
return '1';
|
||||
}
|
||||
elseif ( ($op == '<' || $op == 'is less than' ) && $row[$field] < $value ) {
|
||||
elseif (($op == '<' || $op == 'is less than' ) && $row[$field] < $value) {
|
||||
return '1';
|
||||
}
|
||||
elseif ( ($op == '>' || $op == 'is greater than') && $row[$field] > $value ) {
|
||||
elseif (($op == '>' || $op == 'is greater than') && $row[$field] > $value) {
|
||||
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';
|
||||
}
|
||||
elseif ( ($op == '>=' || $op == 'is greater than or equals') && $row[$field] >= $value ) {
|
||||
elseif (($op == '>=' || $op == 'is greater than or equals') && $row[$field] >= $value) {
|
||||
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';
|
||||
}
|
||||
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';
|
||||
}
|
||||
else {
|
||||
@@ -1049,7 +1049,7 @@ class parseCSV {
|
||||
* @return true of false
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1066,13 +1066,13 @@ class parseCSV {
|
||||
* @return Processed value
|
||||
*/
|
||||
protected function _enclose_value ($value = null, $delimiter = null) {
|
||||
if ( $delimiter === null ) {
|
||||
if (is_null($delimiter)) {
|
||||
$delimiter = $this->delimiter;
|
||||
}
|
||||
if ( $value !== null && $value != '' ) {
|
||||
if ($value !== null && $value != '') {
|
||||
$delimiter_quoted = preg_quote($delimiter, '/');
|
||||
$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 = $this->enclosure.$value.$this->enclosure;
|
||||
}
|
||||
@@ -1090,8 +1090,8 @@ class parseCSV {
|
||||
* @return true or false
|
||||
*/
|
||||
protected function _check_data ($file = null) {
|
||||
if ( empty($this->file_data) ) {
|
||||
if ( $file === null ) $file = $this->file;
|
||||
if (empty($this->file_data)) {
|
||||
if (is_null($file)) $file = $this->file;
|
||||
|
||||
return $this->load_data($file);
|
||||
}
|
||||
@@ -1112,19 +1112,19 @@ class parseCSV {
|
||||
* @return special string used for delimiter selection, or false
|
||||
*/
|
||||
protected function _check_count ($char, $array, $depth, $preferred) {
|
||||
if ( $depth == count($array) ) {
|
||||
if ($depth == count($array)) {
|
||||
$first = null;
|
||||
$equal = null;
|
||||
$almost = false;
|
||||
foreach( $array as $key => $value ) {
|
||||
if ( $first == null ) {
|
||||
foreach ($array as $key => $value) {
|
||||
if ($first == null) {
|
||||
$first = $value;
|
||||
}
|
||||
elseif ( $value == $first && $equal !== false) {
|
||||
elseif ($value == $first && $equal !== false) {
|
||||
$equal = true;
|
||||
}
|
||||
elseif ( $value == $first+1 && $equal !== false ) {
|
||||
$equal = true;
|
||||
elseif ($value == $first+1 && $equal !== false) {
|
||||
$equal = true;
|
||||
$almost = true;
|
||||
}
|
||||
else {
|
||||
@@ -1132,10 +1132,10 @@ class parseCSV {
|
||||
}
|
||||
}
|
||||
|
||||
if ( $equal ) {
|
||||
$match = ( $almost ) ? 2 : 1 ;
|
||||
if ($equal) {
|
||||
$match = ($almost) ? 2 : 1;
|
||||
$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));
|
||||
}
|
||||
@@ -1154,8 +1154,8 @@ class parseCSV {
|
||||
* @return Data from file, or false on failure
|
||||
*/
|
||||
protected function _rfile ($file = null) {
|
||||
if ( is_readable($file) ) {
|
||||
if ( !($fh = fopen($file, 'r')) ) {
|
||||
if (is_readable($file)) {
|
||||
if (!($fh = fopen($file, 'r'))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1179,11 +1179,11 @@ class parseCSV {
|
||||
* @return true or false
|
||||
*/
|
||||
protected function _wfile ($file, $string = '', $mode = 'wb', $lock = 2) {
|
||||
if ( $fp = fopen($file, $mode) ) {
|
||||
if ($fp = fopen($file, $mode)) {
|
||||
flock($fp, $lock);
|
||||
$re = fwrite($fp, $string);
|
||||
$re2 = fclose($fp);
|
||||
if ( $re != false && $re2 != false ) {
|
||||
if ($re != false && $re2 != false) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user