Home >Backend Development >PHP Tutorial >Solution to the problem that the array element obtained by Linux fgetcsv is an empty string_PHP tutorial

Solution to the problem that the array element obtained by Linux fgetcsv is an empty string_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:22:571341browse

But on the server, many use Linux servers, and the source program uses UTF-8, which can easily cause character encoding problems.

If you just transcode the CSV file to UTF-8, there will be no problem on the Windows server. ,

On RedHat5.5, in the array obtained with fgetcsv, if the content of a column is Chinese, the array element corresponding to the column is an empty string, while English is normal.

At this time, you need to set the region:

setlocale(LC_ALL, 'zh_CN.UTF-8');
The code is as follows

Copy code The code is as follows:

// The uploaded CSV file is usually GBK encoded edited with Excel,
// The source code is UTF-8 and needs to be transcoded
file_put_contents($new_file, iconv('GBK', 'UTF-8', file_get_contents($new_file)));

//ini_set('auto_detect_line_endings', true);
// Setting Region: Simplified Chinese, UTF-8 encoding
setlocale(LC_ALL, 'zh_CN.UTF-8');
//Open CSV file
$handle = fopen($new_file, 'r');
//Get column headers
$data_heads = fgetcsv($handle);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324536.htmlTechArticleBut on the server, many use Linux servers and the source program uses UTF-8, which can easily cause character encoding problems. . If you just transcode the CSV file to UTF-8, there will be no problem on the Windows server...
Statement:
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