mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 08:36:39 +00:00
auto readability
This commit is contained in:
@@ -334,31 +334,45 @@ class parseCSV {
|
|||||||
* @param enclosure enclosure character, default is double quote (").
|
* @param enclosure enclosure character, default is double quote (").
|
||||||
* @return delimiter character
|
* @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 ( $file === null ) {
|
||||||
if ( empty($search_depth) ) $search_depth = $this->auto_depth;
|
$file = $this->file;
|
||||||
if ( $enclosure === null ) $enclosure = $this->enclosure;
|
}
|
||||||
|
|
||||||
if ( $preferred === null ) $preferred = $this->auto_preferred;
|
if ( empty($search_depth) ) {
|
||||||
|
$search_depth = $this->auto_depth;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( $enclosure === null ) {
|
||||||
|
$enclosure = $this->enclosure;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( $preferred === null ) {
|
||||||
|
$preferred = $this->auto_preferred;
|
||||||
|
}
|
||||||
|
|
||||||
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 return false;
|
}
|
||||||
} else {
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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 ;
|
||||||
|
|
||||||
@@ -366,25 +380,30 @@ class parseCSV {
|
|||||||
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]++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -392,7 +411,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) ) {
|
||||||
@@ -405,10 +424,11 @@ class parseCSV {
|
|||||||
$this->delimiter = reset($filtered);
|
$this->delimiter = reset($filtered);
|
||||||
|
|
||||||
// parse data
|
// parse data
|
||||||
if ( $parse ) $this->data = $this->parse_string();
|
if ( $parse ) {
|
||||||
|
$this->data = $this->parse_string();
|
||||||
|
}
|
||||||
|
|
||||||
return $this->delimiter;
|
return $this->delimiter;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user