Read csv file data function:
-
- function getData($file) {
- $arr = array();
- if(($handle = fopen($file,"r")) !== FALSE) {
- while(($data = fgetcsv($handle)) !== FALSE) {
-
- $tmp = array();
- foreach($data as $key=>$v) {
- $tmp[] = mb_convert_encoding($v,"UTF -8","gbk"); /*Convert gbk code to utf-8, otherwise garbled characters will appear*/
- }
-
- $arr[] = $tmp;
- }
- }
-
- return $arr;
- }
-
Copy code
Found that the Chinese string read is empty....
Solution: Replace the fgetcsv function with the custom _fgetcsv function
-
- function _fgetcsv(&$handle, $length = null, $d = ',', $e = '"') {
- $d = preg_quote($d);
- $e = preg_quote($ e);
- $_line = "";
- $eof=false;
- while ($eof != true) {
- $_line .= (empty ($length) ? fgets($handle) : fgets($handle, $ length));
- $itemcnt = preg_match_all('/' . $e . '/', $_line, $dummy);
- if ($itemcnt % 2 == 0)
- $eof = true;
- }
- $_csv_line = preg_replace('/(?: |[ ])?$/', $d, trim($_line));
- $_csv_pattern = '/(' . $e . '[^' . $e . ']* (?:' . $e . $e . '[^' . $e . ']*)*' . $e . '|[^' . $d . ']*)' . $d . '/' ; _ Preg_Match_all ($ _ csv_pattern, $ _CSV_LINE, $ _CSV_Matches);
- $ _csv_data = $ _CSV_Matches [1]; nt ($ _ csv_data); $ _csv_i ++) {
- $ _csv_data [ $_csv_i] = preg_replace('/^' . $e . '(.*)' . $e . '$/s', '$1' , $_csv_data[$_csv_i]);
- $_csv_data[$_csv_i] = str_replace($e . $e, $e, $_csv_data[$_csv_i]);
- }
- return empty ($_line) ? false : $_csv_data;
- }
-
-
Copy code
|