Home >Backend Development >PHP Tutorial >How to solve the problem of garbled characters when importing csv files into PHP
This article mainly introduces the solution to the problem of garbled characters when importing csv files into PHP. Friends who need it can refer to it.
Today I mainly want to write a method for importing csv files into PHP. In fact, a search on the Internet A lot. All can be implemented how to import. But I encountered two problems when importing. One was that the test had garbled characters when writing code on Windows, and then it was solved. The second one is that when it was submitted to the Linux system, garbled characters occurred again. I didn't know the reason for the garbled code at first. At first I thought it was an error in the code svn submission. In the end, I asked a question in one of my groups. A friend of mine works in phpcms. He said that he encountered an error in svn submission from Windows. When submitting to Linux, errors always occurred at first. Later, the cause was found to be garbled characters. Let’s get right to the point and see how to solve these two problems!
Solution to the problem:
When PHP reads a csv file, Chinese cannot be read on Windows. I immediately thought of a function mb_convert_encoding(); make the following settings $str = mb_convert_encoding ($str, "UTF-8", "GBK"); Then that's it. Of course, you can also use iconv(); to set iconv('GBK',"UTF-8//TRANSLIT//IGNORE",$str); as follows to solve the problem of garbled characters on Windows.
Solution to problem two:
PHP reads the csv file, but the Chinese cannot be read on Linux. I found the solution after Baidu and Google.
Just added it One line of code setlocale(LC_ALL, 'zh_CN'); Yes, it will blind your eyes. It's that simple, and if you didn't know, you could spend a lot of time figuring it out.
PHP setlocale() function explanation
Definition and usage
setlocale() function sets regional information (regional information).
Regional information is language, currency, time and other information for a geographical area. This function returns the current locale, or false on failure.
The following are commonly used region identifiers for collecting data:
zh_CN GB2312 en_US.UTF-8 UTF-8 zh_TW BIG5 zh_HK BIG5-HKSCS zh_TW.EUC-TW EUC-TW zh_TW.UTF-8 UTF-8 zh_HK.UTF-8 UTF-8 zh_CN.GBK GBK
For example,
utf-8: setlocale(LC_ALL, 'en_US.UTF-8′);
Simplified: setlocale(LC_ALL, 'zh_CN');
The reason why I tell you about the setlocale() function is because when I imported the csv file into the Linux system, garbled characters occurred, including the use of mb_convert_encoding( ) and iconv() functions failed to solve the final problem. Finally, I added this sentence setlocale(LC_ALL, 'zh_CN'); in front of the code at the beginning of importing the csv file and it was easily done. Then I searched for information and found that the fgetcsv() function is sensitive to locale settings. For example, if LANG is set to en_US.UTF-8, single-byte encoded files will have read errors, so we need to set the culture. Specially shared with everyone.
I also tried to use the following code but could not get it. These are the header settings for generating csv files. It might not work for me, but it might work for you. So I sorted it out and tried my best to help colleagues who encountered garbled characters when importing csv files, because it was really difficult to deal with it when there was no other way. Everyone can try it! There is always one that belongs to you.
<?php $csvContent="csvzero,csvone,csvtwo,csvthree,csvfour,csvfive"; header("Content-Type: application/vnd.ms-excel; charset=GB2312"); header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Content-Disposition: attachment;filename=CSV数据.csv "); header("Content-Transfer-Encoding: binary "); $csvContent = iconv("utf-8","gb2312",$csvContent); echo $csvContent; exit; ?>
Let’s take a closer look at the code for php to import csv files:
A brief introduction to the two functions,
The character encoding detected by mb_detect_encoding() , or returns FALSE when the encoding of the specified string cannot be detected.
fgetcsv() function reads a line from the file pointer and parses the CSV field. Similar to fgets(), except that fgetcsv() parses the read line and finds the fields in CSV format, then returns an array containing those fields. fgetcsv() returns FALSE on error, including when the end of file is encountered.
Note: As of PHP 4.3.5, the operation of fgetcsv() is binary safe.
Note: Empty lines in the CSV file will be returned as an array containing a single null field and will not be treated as an error.
Note: This function is sensitive to locale settings. For example, if LANG is set to en_US.UTF-8, single-byte encoded files will have read errors.
Note: If you encounter that PHP cannot recognize the line ending characters of Macintosh files when reading files, you can activate the auto_detect_line_endings runtime configuration option.
<?php setlocale(LC_ALL, 'zh_CN'); //设置地区信息(地域信息) $file = $_FILES['files']; $file_type = substr(strstr($file['name'],'.'),1); if ($file_type != 'csv'){ echo "<script type=\"text/javascript\">alert(\"文件格式错误,请重新上传!\"); </script>"; exit; } $handle = fopen($file['tmp_name'],"r"); $file_encoding = mb_detect_encoding($handle); if ($file_encoding != 'ASCII'){ echo "<script type=\"text/javascript\">alert(\"文件编码错误,请重新上传!\"); </script>"; exit; } $row = 0; $str=""; $sy=""; while ($data = fgetcsv($handle,1000,',')){ $row++; if ($row == 0) continue; $num = count($data); for ($i=0; $i<$num; $i++){ $str = (string)$data[$i].'|'; $str = mb_convert_encoding($str, "UTF-8", "GBK"); //已知源码为GBK,转换为utf-8 $sy .= $str; //我这里做的比较复杂,是用'|'将csv文件里面的内容用'|'全部拼起来,因为我导入的是商品信息,需要根据用户需 //要导入的数据去定义哪些数据是需要导入的。 } } if ($sy) { $sy = rtrim($sy, '|'); } $arr = explode('|',$sy); $key = array_slice($arr,0,$num); //这个数组就是csv文件里面标题,就是商品id,标题,卖点等等的数据 $skey = array(); $length = array(); $co = count($arr); $p = $co/$num; //求出要取出的数据的长度 for($j=0;$j<$p;$j++){ $offset=($j-1)*$num; //偏移量,就像分页一样,我这里根据偏移量取出的一个数组就是一个商品的信息。 if($j==0){ $length[] = array_slice($arr,0,$num); }else{ $length[] = array_slice($arr,$num+$offset,$num);//取出有哪些字段和商品 } } $arrtitle = array(); $arrfileds = array(); $arrtagname = DB::select('字段标识', '字段名称')->from('字段表')->fetch_all(); foreach ($arrtagname as $value) { $arrfileds[$value['fileds_tags']] = $value['fileds_name']; } foreach ($fileds as $v) { $temarr= explode('-', $v); if (isset($temarr[0]) && !empty($temarr[0])) { if (isset($temarr[1]) && !empty($temarr[1])) { if ($temarr[1] == 'wenben') { $arrtitle[] = $arrfileds[$temarr[0]].'文本'; } } else { if ($temarr[0] != 'pic') { //是取出字段是图片就给去掉 $arrtitle[] = $arrfileds[$temarr[0]]; } } } } $skey = array(); $order = array(); $order[] = 'act_tag'; $order[] = 'channel_tag'; $order[] = 'created_time'; $order[] = 'orderby'; $rows =''; $f = $co/$num;//求出有多少件商品 for($p=0;$p<count($arrtitle);$p++){ //这里就是根据自己的需求查出自己需要的数据,通过用户需要的商品字段标识查出表里相对应的英文标识。 $skey[]= DB::select('字段标识')->from('字段表')->where('字段名称', '=', $arrtitle[$p])->fetch_row(); $rows .= $skey[$p]['字段标识'].'|'; } if($rows){ $rows = rtrim($rows,'|'); } if(!empty($rows)){ $exrows = explode('|',$rows); }else{ $exrows = array(); } $skeys = array_merge($order,$exrows); $count1 = count($skeys); //字段的个数 if(!empty($length)){ for($x=1;$x<$f;$x++){ //求出有多少件商品就的循环多少次 $orders = array(); $orders[] = $act_tag; $orders[] = $channel_tag; $orders[] = time(); $newlen = array_merge($orders,$length[$x]); if($count1 !== count($newlen)){ //如果商品字段的长度和商品的长度不等就证明用户有哪个字段没录入 $newrs = array(); echo "<script type=\"text/javascript\">alert(\"<font color=#f00;>".'请检查第,'.($x-1).'件商品!'.'导入失败!'."</font>"); </script>"; fclose($handle); exit(); }else{ //start $arrimport = array_combine($skeys,$newlen); //如果两个数组是相等的我就合并数组,并把导入csv里面的日期改为时间戳存储到数据库 if(!empty($arrimport['start_time'])){ $sta = strtotime($arrimport['start_time']); }else{ $sta=(int)0; } if(!empty($arrimport['end_time'])){ $end = strtotime($arrimport['end_time']); }else{ $end=(int)0; } $arrtime=array('start_time'=>$sta,'end_time'=>$end); if(!empty($arrimport['start_time']) && !empty($arrimport['end_time'])){ $newrs=array_merge($arrimport,$arrtime); }else{ $newrs = array(); echo "<script type=\"text/javascript\">alert(\"<font color=#f00;>".'请检查第,'.($x-1).'件商品!'.'导入失败!'."</font>"); </script>"; fclose($handle); exit(); } if(count($skeys) == count($newrs)){ DB::insert('商品表', array_values($skeys)) ->values(array_values($newrs)) ->execute(); } } //end } } if($row-1==(int)0){ echo "<script type=\"text/javascript\">alert(\"<font color=#f00;>".'您导入的商品为空!'."</font>"); </script>"; }else{ echo "<script type=\"text/javascript\">alert(\"<font color=#f00;>".'成功导入'."<font color=#f00;>".($row-1)."</font>".'件商品!'."</font>"); } fclose($handle); } ?>
The above is the csv import process that I need to do for my work. It may be different from your import method, but some codes will always be helpful to you!
The following is a simple import:
<form enctype="multipart/form-data" action="import.php" method="POST"> 导入模板 <label for="文件选择">文件选择:</label><input name="csv_goods" type="file" /> <input type="submit" value="导入" name="import" /> </form> <?php if (isset($_POST['import'])){ $file = $_FILES['csv_goods']; $file_type = substr(strstr($file['name'],'.'),1); // 检查文件格式 if ($file_type != 'csv'){ echo '文件格式不对,请重新上传!'; exit; } $handle = fopen($file['tmp_name'],"r"); $file_encoding = mb_detect_encoding($handle); // 检查文件编码 if ($file_encoding != 'ASCII'){ echo '文件编码错误,请重新上传!'; exit; } $row = 0; while ($data = fgetcsv($handle,1000,',')){ //echo "<font color=red>$row</font>"; //可以知道总共有多少行 $row++; if ($row == 1) continue; $num = count($data); // 这里会依次输出每行当中每个单元格的数据 for ($i=0; $i<$num; $i++){ echo $data[$i]."<br>"; // 在这里对数据进行处理 } } fclose($handle); } ?>
The above is the entire content of this article. I hope it will be helpful to everyone’s learning. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Introduction to the calling interface and common functions of PHP encapsulated curl
The above is the detailed content of How to solve the problem of garbled characters when importing csv files into PHP. For more information, please follow other related articles on the PHP Chinese website!