mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 08:36:39 +00:00
conditions, limit and offset are functional. conditions needs a rewrite tho.
added examples to trunk. git-svn-id: http://parsecsv-for-php.googlecode.com/svn/trunk@13 339761fc-0c37-0410-822d-8b8cac1f6a97
This commit is contained in:
103
parsecsv.lib.php
103
parsecsv.lib.php
@@ -76,6 +76,7 @@ class parseCSV {
|
||||
|
||||
/**
|
||||
* Configuration
|
||||
* - set these options with $object->var_name = 'value';
|
||||
*/
|
||||
|
||||
# use first line/entry as field names
|
||||
@@ -92,6 +93,15 @@ class parseCSV {
|
||||
var $delimiter = ',';
|
||||
var $enclosure = '"';
|
||||
|
||||
# basic conditional matching against values. rows that don't match are ignored
|
||||
var $conditions = array();
|
||||
|
||||
# number of rows to ignore from beginning of data
|
||||
var $offset = null;
|
||||
|
||||
# limits the number of returned rows to specified amount
|
||||
var $limit = null;
|
||||
|
||||
# number of rows to analyze when attempting to auto-detect delimiter
|
||||
var $auto_depth = 15;
|
||||
|
||||
@@ -137,7 +147,10 @@ class parseCSV {
|
||||
* @param input CSV file or string
|
||||
* @return nothing
|
||||
*/
|
||||
function parseCSV ($input = null) {
|
||||
function parseCSV ($input = null, $offset = null, $limit = null, $conditions = array()) {
|
||||
if ( $offset !== null ) $this->offset = $offset;
|
||||
if ( $limit !== null ) $this->limit = $limit;
|
||||
if ( count($conditions) > 0 ) $this->conditions = $conditions;
|
||||
if ( !empty($input) ) $this->parse($input);
|
||||
}
|
||||
|
||||
@@ -151,8 +164,11 @@ class parseCSV {
|
||||
* @param input CSV file or string
|
||||
* @return nothing
|
||||
*/
|
||||
function parse ($input = null) {
|
||||
function parse ($input = null, $offset = null, $limit = null, $conditions = array()) {
|
||||
if ( !empty($input) ) {
|
||||
if ( $offset !== null ) $this->offset = $offset;
|
||||
if ( $limit !== null ) $this->limit = $limit;
|
||||
if ( count($conditions) > 0 ) $this->conditions = $conditions;
|
||||
if ( is_readable($input) ) {
|
||||
$this->data = $this->parse_file($input);
|
||||
} else {
|
||||
@@ -367,9 +383,9 @@ class parseCSV {
|
||||
|
||||
// walk through each character
|
||||
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 ;
|
||||
$ch = $data{$i};
|
||||
$nch = ( isset($data{$i+1}) ) ? $data{$i+1} : false ;
|
||||
$pch = ( isset($data{$i-1}) ) ? $data{$i-1} : false ;
|
||||
|
||||
// open and closing quotes
|
||||
if ( $ch == $this->enclosure && (!$enclosed || $nch != $this->enclosure) ) {
|
||||
@@ -390,22 +406,29 @@ class parseCSV {
|
||||
|
||||
// end of row
|
||||
if ( $ch == "\n" || $ch == "\r" ) {
|
||||
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]]) ) {
|
||||
$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++ ) {}
|
||||
$rows[$row[$this->sort_by].'_'.$sn] = $row;
|
||||
} else $rows[$row[$this->sort_by]] = $row;
|
||||
} else $rows[] = $row;
|
||||
if ( $this->validate_offset($row_count) ) {
|
||||
if ( $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]]) ) {
|
||||
$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++ ) {}
|
||||
$rows[$row[$this->sort_by].'_'.$sn] = $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;
|
||||
}
|
||||
}
|
||||
|
||||
// append character to current field
|
||||
} else {
|
||||
@@ -415,6 +438,9 @@ class parseCSV {
|
||||
$this->titles = $head;
|
||||
if ( !empty($this->sort_by) ) {
|
||||
( $this->sort_reverse ) ? krsort($rows) : ksort($rows) ;
|
||||
if ( $this->offset !== null || $this->limit !== null ) {
|
||||
$rows = array_slice($rows, ($this->offset === null ? 0 : $this->offset) , $this->limit, true);
|
||||
}
|
||||
}
|
||||
return $rows;
|
||||
}
|
||||
@@ -463,6 +489,49 @@ class parseCSV {
|
||||
// ----- [ Internal Functions ] -----------------
|
||||
// ==============================================
|
||||
|
||||
/**
|
||||
* Validate a row against specified conditions
|
||||
* @param row array with values from a row
|
||||
* @param conditions specified conditions that the row must match
|
||||
* @return true of false
|
||||
*/
|
||||
function validate_row_conditions ($row = array(), $conditions = array()) {
|
||||
if ( !empty($row) ) {
|
||||
if ( !empty($conditions) ) {
|
||||
foreach( $conditions as $key => $value ) {
|
||||
if ( array_key_exists($key, $row) ) {
|
||||
if ( is_array($value) ) {
|
||||
$match = array();
|
||||
foreach( $value as $k => $v ) {
|
||||
$v = preg_quote($v, '/');
|
||||
$match[] = '(?:'.str_replace('\*', '.*?', $v).')';
|
||||
}
|
||||
$match = implode('|', $match);
|
||||
} else {
|
||||
$match = preg_quote($value, '/');
|
||||
$match = str_replace('\*', '.*?', $match);
|
||||
}
|
||||
if ( !preg_match('/^'.$match.'$/i', $row[$key]) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates if the row is within the offset or not if sorting is disabled
|
||||
* @param current_row the current row number being processed
|
||||
* @return true of false
|
||||
*/
|
||||
function validate_offset ($current_row) {
|
||||
if ( $this->sort_by === null && $this->offset !== null && $current_row < $this->offset ) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enclose values if needed
|
||||
* - only used by unparse()
|
||||
|
||||
Reference in New Issue
Block a user