mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 00:36:38 +00:00
auto readability
This commit is contained in:
@@ -334,31 +334,45 @@ class parseCSV {
|
||||
* @param enclosure enclosure character, default is double quote (").
|
||||
* @return delimiter character
|
||||
*/
|
||||
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 ( $file === null ) $file = $this->file;
|
||||
if ( empty($search_depth) ) $search_depth = $this->auto_depth;
|
||||
if ( $enclosure === null ) $enclosure = $this->enclosure;
|
||||
if ( $file === null ) {
|
||||
$file = $this->file;
|
||||
}
|
||||
|
||||
if ( empty($search_depth) ) {
|
||||
$search_depth = $this->auto_depth;
|
||||
}
|
||||
|
||||
if ( $enclosure === null ) {
|
||||
$enclosure = $this->enclosure;
|
||||
}
|
||||
|
||||
if ( $preferred === null ) $preferred = $this->auto_preferred;
|
||||
if ( $preferred === null ) {
|
||||
$preferred = $this->auto_preferred;
|
||||
}
|
||||
|
||||
if ( empty($this->file_data) ) {
|
||||
if ( $this->_check_data($file) ) {
|
||||
$data = &$this->file_data;
|
||||
} else return false;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
$data = &$this->file_data;
|
||||
}
|
||||
|
||||
$chars = array();
|
||||
$strlen = strlen($data);
|
||||
$chars = array();
|
||||
$strlen = strlen($data);
|
||||
$enclosed = false;
|
||||
$n = 1;
|
||||
$to_end = true;
|
||||
$n = 1;
|
||||
$to_end = true;
|
||||
|
||||
// walk specific depth finding posssible delimiter characters
|
||||
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 ;
|
||||
|
||||
@@ -366,25 +380,30 @@ class parseCSV {
|
||||
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 ) {
|
||||
}
|
||||
elseif ( ($ch == "\n" && $pch != "\r" || $ch == "\r") && !$enclosed ) {
|
||||
if ( $n >= $search_depth ) {
|
||||
$strlen = 0;
|
||||
$to_end = false;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
$n++;
|
||||
}
|
||||
|
||||
// count character
|
||||
} elseif (!$enclosed) {
|
||||
}
|
||||
elseif (!$enclosed) {
|
||||
if ( !preg_match('/['.preg_quote($this->auto_non_chars, '/').']/i', $ch) ) {
|
||||
if ( !isset($chars[$ch][$n]) ) {
|
||||
$chars[$ch][$n] = 1;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
$chars[$ch][$n]++;
|
||||
}
|
||||
}
|
||||
@@ -392,7 +411,7 @@ 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) ) {
|
||||
@@ -405,10 +424,11 @@ class parseCSV {
|
||||
$this->delimiter = reset($filtered);
|
||||
|
||||
// parse data
|
||||
if ( $parse ) $this->data = $this->parse_string();
|
||||
if ( $parse ) {
|
||||
$this->data = $this->parse_string();
|
||||
}
|
||||
|
||||
return $this->delimiter;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user