-
-
//ini_set('memory_limit', '-1'); // 如果csv比較大的話,可以加。
- /*
- * $file : csv file
- * $csvDataArr : header of csv table, eg: arary('name','sex','age') or array(0,1,2)
- * $specialhtml : whether do you want to convert special characters to html entities ?
- * $removechar : which type do you want to remove special characters in array keys, manual or automatical ?
- * edit http://bbs.it-home.org
- */
- class csv_to_array
- {
- private $counter;
- private $handler;
- private $length;
- private $file;
- private $seprator;
- private $specialhtml;
- private $removechar = 'manual';
- private $csvDataArr;
- private $csvData = array();
-
- function __construct($file = '', $csvDataArr = '', $specialhtml = true, $length = 1000, $seprator = ',')
- {
- $this->counter = 0;
- $this->length = $length;
- $this->file = $file;
- $this->seprator = $seprator;
- $this->specialhtml = $specialhtml;
- $this->csvDataArr = is_array($csvDataArr) ? $csvDataArr : array();
- $this->handler = fopen($this->file, "r");
- }
-
- function get_array()
- {
- $getCsvArr = array();
- $csvDataArr = array();
- while(($data = fgetcsv($this->handler, $this->length, $this->seprator)) != FALSE)
- {
- $num = count($data);
- $getCsvArr[$this->counter] = $data;
- $this->counter++;
- }
- if(count($getCsvArr) > 0)
- {
- $csvDataArr = array_shift($getCsvArr);
- if($this->csvDataArr) $csvDataArr = $this->csvDataArr;
-
- $counter = 0;
- foreach($getCsvArr as $csvValue)
- {
- $totalRec = count($csvValue);
- for($i = 0; $i < $totalRec ; $i++)
- {
- $key = $this->csvDataArr ? $csvDataArr[$i] : $this->remove_char($csvDataArr[$i]);
- if($csvValue[$i]) $this->csvData[$counter][$key] = $this->put_special_char($csvValue[$i]);
- }
- $counter++;
- }
- }
- return $this->csvData;
- }
-
- function put_special_char($value)
- {
- return $this->specialhtml ? str_replace(array('&','" ','\'','<','>'),array('&','"',''','<','>'),$value) : $value;
- }
-
- function remove_char($value)
- {
- $result = $this->removechar == 'manual' ? $this->remove_char_manual($value) : $this->remove_char_auto($value);
- return str_replace(' ','_',trim($result));
- }
-
- private function remove_char_manual($value)
- {
- return str_replace(array('&','"','\'','<','>','(',')','%'),'',trim($value));
- }
-
- private function remove_char_auto($str,$x=0)
- {
- $x==0 ? $str=$this->make_semiangle($str) : '' ;
- eregi('[[:punct:]]',$str,$arr);
- $str = str_replace($arr[0],'',$str);
-
- return eregi('[[:punct:]]',$str) ? $this->remove_char_auto($str,1) : $str;
- }
-
- private function make_semiangle($str)
- {
- $arr = array('0' => '0', '1' => '1', '2' => ; ' 2', '3' => '4',
- '5', '6' => '6', '7' ' => '7', '8' => '8', '9' => 'A' => 'B', 'C' => 'D', 'E' => 'E',
- 'F', 'G' => 'G', 'H' => 'I', 'J' => 'J',
- 'K', 'L' => 'L', 'M' => 'N', 'O' => 'O',
- 'P', 'Q' => 'R', 'S' => 'T',
- 'U' => 'U', 'V' => 'V', 'W' => 'X ', 'Y' => 'Y',
- 'Z' => 'Z', 'a' => 'b', 'c' => 'c', 'd' => 'e' = > 'f' => 'g', 'h' => 'i',
- 'j', 'k' => 'l', 'm' => ',
- 'o' = > ; 'o', 'p' => 'p', 'q' =>; 'q', 'r' => 'r', 's' => 's',
- 't' = > ; 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',
- 'y' => ; 'y', 'z' => 'z',
- '(' =>; '(', ')' => ')', '〔' => '[', ']' => ' ]', '【' => '[',
- '】' => ']', '〖' => '[', '〗' => ']', '"' => '[ ' , '"' => ']',
- ''' => '[', ''' => ']', '{' => '{', '}' => '}'; , '《' => ' '》' => '>',
- '%' =>; '%', '+' => '+', '—' => ' - ', '-' => '-', '~' => '-',
- ':' =>; ':', '。 , ',' => '.', '、' => '.',
- ';' => ',', '? ' => '?', '! ...' => '-', '``' => '|',
- '"' => '"', ''' => '`', ''' => '`' , '|' => '|', '〃' => '"',
- '' => ' ','$'=>'$','@'=>'@','#' = >'#','^'=>'^','&'=>'&','*'=>'*');
-
- return strtr($str, $arr);
- }
-
- function __destruct(){
- fclose($this->handler);
- }
- }
- // 範例:
- $csv = new csv_to_array('user.csv');
- echo "
"; print_r($csv->get_array());迴聲「 登入後複製 」;
- ?>
-
-
複製程式碼
2、使用一般函數
或
-
- $arrCSV = array();
- // 開啟CSV
- if (($handle = fopen("user.csv", "r")) !==FALSE) {
- //將父數組鍵設為0
- $key = 0;
- // 當有資料可用時,使用分隔符號(,) 無限次循環(0)
- while (($data = fgetcsv($ handle, 0, ",")) !==FALSE) {
- // 統計每行的鍵總數
- $c = count($data);
- //填入陣列
- for ( $x=0;$x $key++;
- } // 結束while
- / / 關閉CSV 檔案
- fclose($handle);
- } // 結束if
- echo "
"; print_r($arrCSV);迴聲「 登入後複製 」;
- ?>
複製程式碼
至於哪種工作用,看自己的實際需求與個人愛好了,實際中csv轉數組的需求還是很旺,建議大家多練習,多掌握。
|