From 75038f9d2f7b6f8f0cc4233142cd85e78df3907c Mon Sep 17 00:00:00 2001 From: William Knauss Date: Thu, 6 Feb 2014 18:27:54 -0500 Subject: [PATCH] auto readability --- parsecsv.lib.php | 60 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/parsecsv.lib.php b/parsecsv.lib.php index ad0d4d6..cd65dc3 100644 --- a/parsecsv.lib.php +++ b/parsecsv.lib.php @@ -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; - }