How to solve the problem of garbled characters when importing csv files into PHP

不言
Release: 2023-04-02 09:10:01
Original
2619 people have browsed it

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
Copy after login

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.

Copy after login

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.

alert(\"文件格式错误,请重新上传!\"); "; 
exit; 
} 
$handle = fopen($file['tmp_name'],"r"); 
$file_encoding = mb_detect_encoding($handle); 
if ($file_encoding != 'ASCII'){ 
echo ""; 
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;$pfrom('字段表')->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 ""; 
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 ""; 
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 ""; 
}else{ 
echo "
        
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!