Home Backend Development PHP Tutorial How to solve the problem of garbled characters when importing csv files into PHP

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

Jul 03, 2018 pm 04:15 PM
Garbled characters

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.

<?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; 
?>
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.

<?php 
setlocale(LC_ALL, &#39;zh_CN&#39;); //设置地区信息(地域信息) 
$file = $_FILES[&#39;files&#39;]; 
$file_type = substr(strstr($file[&#39;name&#39;],&#39;.&#39;),1); 
if ($file_type != &#39;csv&#39;){ 
echo "<script type=\"text/javascript\">alert(\"文件格式错误,请重新上传!\"); </script>"; 
exit; 
} 
$handle = fopen($file[&#39;tmp_name&#39;],"r"); 
$file_encoding = mb_detect_encoding($handle); 
if ($file_encoding != &#39;ASCII&#39;){ 
echo "<script type=\"text/javascript\">alert(\"文件编码错误,请重新上传!\"); </script>"; 
exit; 
} 
$row = 0; 
$str=""; 
$sy=""; 
while ($data = fgetcsv($handle,1000,&#39;,&#39;)){ 
$row++; 
if ($row == 0) 
continue; 
$num = count($data); 
for ($i=0; $i<$num; $i++){ 
$str = (string)$data[$i].&#39;|&#39;; 
$str = mb_convert_encoding($str, "UTF-8", "GBK"); //已知源码为GBK,转换为utf-8 
$sy .= $str; //我这里做的比较复杂,是用&#39;|&#39;将csv文件里面的内容用&#39;|&#39;全部拼起来,因为我导入的是商品信息,需要根据用户需 
//要导入的数据去定义哪些数据是需要导入的。 
} 
} 
if ($sy) { $sy = rtrim($sy, &#39;|&#39;); } 
$arr = explode(&#39;|&#39;,$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(&#39;字段标识&#39;, &#39;字段名称&#39;)->from(&#39;字段表&#39;)->fetch_all(); 
foreach ($arrtagname as $value) { 
$arrfileds[$value[&#39;fileds_tags&#39;]] = $value[&#39;fileds_name&#39;]; 
} 
foreach ($fileds as $v) 
{ 
$temarr= explode(&#39;-&#39;, $v); 
if (isset($temarr[0]) && !empty($temarr[0])) { 
if (isset($temarr[1]) && !empty($temarr[1])) { 
if ($temarr[1] == &#39;wenben&#39;) { 
$arrtitle[] = $arrfileds[$temarr[0]].&#39;文本&#39;; 
} 
} else { 
if ($temarr[0] != &#39;pic&#39;) { //是取出字段是图片就给去掉 
$arrtitle[] = $arrfileds[$temarr[0]]; 
} 
} 
} 
} 
$skey = array(); 
$order = array(); 
$order[] = &#39;act_tag&#39;; 
$order[] = &#39;channel_tag&#39;; 
$order[] = &#39;created_time&#39;; 
$order[] = &#39;orderby&#39;; 
$rows =&#39;&#39;; 
$f = $co/$num;//求出有多少件商品 
for($p=0;$p<count($arrtitle);$p++){ 
//这里就是根据自己的需求查出自己需要的数据,通过用户需要的商品字段标识查出表里相对应的英文标识。 
$skey[]= DB::select(&#39;字段标识&#39;)->from(&#39;字段表&#39;)->where(&#39;字段名称&#39;, &#39;=&#39;, $arrtitle[$p])->fetch_row(); 
$rows .= $skey[$p][&#39;字段标识&#39;].&#39;|&#39;; 
} 
if($rows){ $rows = rtrim($rows,&#39;|&#39;); } 
if(!empty($rows)){ $exrows = explode(&#39;|&#39;,$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;>".&#39;请检查第,&#39;.($x-1).&#39;件商品!&#39;.&#39;导入失败!&#39;."</font>"); </script>"; 
fclose($handle); 
exit(); 
}else{ //start 
$arrimport = array_combine($skeys,$newlen); //如果两个数组是相等的我就合并数组,并把导入csv里面的日期改为时间戳存储到数据库 
if(!empty($arrimport[&#39;start_time&#39;])){ $sta = strtotime($arrimport[&#39;start_time&#39;]); }else{ $sta=(int)0; } 
if(!empty($arrimport[&#39;end_time&#39;])){ $end = strtotime($arrimport[&#39;end_time&#39;]); }else{ $end=(int)0; } 
$arrtime=array(&#39;start_time&#39;=>$sta,&#39;end_time&#39;=>$end); 
if(!empty($arrimport[&#39;start_time&#39;]) && !empty($arrimport[&#39;end_time&#39;])){ 
$newrs=array_merge($arrimport,$arrtime); 
}else{ 
$newrs = array(); 
echo "<script type=\"text/javascript\">alert(\"<font color=#f00;>".&#39;请检查第,&#39;.($x-1).&#39;件商品!&#39;.&#39;导入失败!&#39;."</font>"); </script>"; 
fclose($handle); 
exit(); 
} 
if(count($skeys) == count($newrs)){ 
DB::insert(&#39;商品表&#39;, array_values($skeys)) 
->values(array_values($newrs)) 
->execute(); 
} 
} //end 
} 
} 
if($row-1==(int)0){ 
echo "<script type=\"text/javascript\">alert(\"<font color=#f00;>".&#39;您导入的商品为空!&#39;."</font>"); </script>"; 
}else{ 
echo "<script type=\"text/javascript\">alert(\"<font color=#f00;>".&#39;成功导入&#39;."<font color=#f00;>".($row-1)."</font>".&#39;件商品!&#39;."</font>"); 
} 
fclose($handle); 
} 
?>
Copy after login

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[&#39;import&#39;])){ 
$file = $_FILES[&#39;csv_goods&#39;]; 
$file_type = substr(strstr($file[&#39;name&#39;],&#39;.&#39;),1); 
// 检查文件格式 
if ($file_type != &#39;csv&#39;){ 
echo &#39;文件格式不对,请重新上传!&#39;; 
exit; 
} 
$handle = fopen($file[&#39;tmp_name&#39;],"r"); 
$file_encoding = mb_detect_encoding($handle); 
// 检查文件编码 
if ($file_encoding != &#39;ASCII&#39;){ 
echo &#39;文件编码错误,请重新上传!&#39;; 
exit; 
} 
$row = 0; 
while ($data = fgetcsv($handle,1000,&#39;,&#39;)){ 
//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); 
} 
?>
Copy after login

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 php processing Chinese string interception (mb_substr) and obtaining the number of characters in Chinese strings

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve garbled word page numbers How to solve garbled word page numbers Jun 25, 2023 pm 03:23 PM

Solution to garbled word page numbers: 1. Open the word document and click the "File" option in the upper left corner; 2. Select the "More" option, and then click the "Options" button; 3. Select "Advanced" in the word options; 4. . Find "Show field codes instead of field values" in "Show document content", remove the check in front, and click OK to return to the home page.

How to solve Chinese garbled characters in Linux How to solve Chinese garbled characters in Linux Feb 21, 2024 am 10:48 AM

The Linux Chinese garbled problem is a common problem when using Chinese character sets and encodings. Garbled characters may be caused by incorrect file encoding settings, system locale not being installed or set, and terminal display configuration errors, etc. This article will introduce several common workarounds and provide specific code examples. 1. Check the file encoding setting. Use the file command to view the file encoding. Use the file command in the terminal to view the encoding of the file: file-ifilename. If there is "charset" in the output

How to solve tomcat startup garbled code How to solve tomcat startup garbled code Dec 26, 2023 pm 05:21 PM

Solutions to garbled tomcat startup: 1. Modify Tomcat's conf configuration file; 2. Modify the system language; 3. Modify the command line window encoding; 4. Check the Tomcat server configuration; 5. Check the project encoding; 6. Check the log file; 7 , try other solutions. Detailed introduction: 1. Modify Tomcat's conf configuration file, open Tomcat's conf directory, find the "logging.properties" file, etc.

How to solve the problem of Chinese garbled characters in Windows 10 How to solve the problem of Chinese garbled characters in Windows 10 Jan 16, 2024 pm 02:21 PM

In the Windows 10 system, garbled characters are common. The reason behind this is often that the operating system does not provide default support for some character sets, or there is an error in the set character set options. In order to prescribe the right medicine, we will analyze the actual operating procedures in detail below. How to solve Windows 10 garbled code 1. Open settings and find "Time and Language" 2. Then find "Language" 3. Find "Manage Language Settings" 4. Click "Change System Regional Settings" here 5. Check the box as shown and click Just make sure.

What to do if linux tty has Chinese garbled characters What to do if linux tty has Chinese garbled characters Mar 16, 2023 am 09:20 AM

Solution to Chinese garbled characters in Linux tty: 1. Download the font fbterm through the "sudo apt-get install fbterm" command; 2. Execute the "sudo fbterm" command; 3. Change the font and font size to "font-names=Ubuntu Mono font- size=14” is enough.

How to solve the problem of garbled characters in win11 system documents How to solve the problem of garbled characters in win11 system documents Jun 29, 2023 pm 06:29 PM

How to solve the problem of garbled text documents in win11? When many users use the win11 system, text documents are garbled and cannot be read normally. Many friends do not know how to solve this problem. In fact, this method is not difficult. Below, the editor has compiled the steps to solve the problem of garbled Windows 11 system documents. I hope it can bring you some inspiration! Steps to solve garbled Windows 11 system documents: 1. First, open the control panel of win11, enter control panel in the search box below, and click Search to enter the control panel. 2. After entering the panel, find the clock and area and click to enter, then click on the area option. 3. After entering, click on the management panel, and then click on Change system regional settings.

How to solve filezilla garbled characters How to solve filezilla garbled characters Nov 20, 2023 am 10:16 AM

Solutions to filezilla garbled characters include: 1. Check the encoding settings; 2. Check the file itself; 3. Check the server configuration; 4. Try other transfer tools; 5. Update the software version; 6. Check for network problems; 7. Seek technical support. To solve the problem of FileZilla garbled characters, you need to start from multiple aspects, gradually investigate the cause of the problem, and take corresponding measures to repair it.

Editing method to solve the problem of garbled characters when opening dll files Editing method to solve the problem of garbled characters when opening dll files Jan 06, 2024 pm 07:53 PM

When many users use computers, they will find that there are many files with the suffix dll, but many users do not know how to open such files. For those who want to know, please take a look at the following details. Tutorial~How to open and edit dll files: 1. Download a software called "exescope" and download and install it. 2. Then right-click the dll file and select "Edit resources with exescope". 3. Then click "OK" in the pop-up error prompt box. 4. Then on the right panel, click the "+" sign in front of each group to view the content it contains. 5. Click on the dll file you want to view, then click "File" and select "Export". 6. Then you can

See all articles