What to do if the csv file exported by yii2 is garbled?
Jan 13, 2020 pm 03:10 PMyii exports the CSV code as follows:
/** * 導(dǎo)出csv * @author yhdsir * @param array $parameter header 表頭 * @param array $parameter data 數(shù)據(jù) * @param string $filename 導(dǎo)出名字 */ public function export($parameter, $filename = '') { if (empty($filename)) { $filename = date('Y-m-d_H-i-s'); } $filename = str_replace(array('"', "'", ' ', ','), '_', $filename) . '.csv'; if (is_array($parameter)) { header('Content-Type: application/vnd.ms-excel'); header('Cache-Control: max-age=0'); header("Content-Disposition: attachment;filename={$filename}"); $fp = fopen('php://output', 'w'); //fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF)); // 添加 BOM if (!empty($parameter['header']) && is_array($parameter['header'])) { foreach ($parameter['header'] as $i => $v) { // CSV的Excel支持GBK編碼,一定要轉(zhuǎn)換,否則亂碼 // $head[$i] = iconv('utf-8', 'gbk', $v); $parameter['header'][$i] = iconv('utf-8', 'gb2312//TRANSLIT//IGNORE', $v); } // 將數(shù)據(jù)通過fputcsv寫到文件句柄 fputcsv($fp, $parameter['header']); } if (isset($parameter['data'])) { foreach ($parameter['data'] as $row) { foreach ($row as $i => $v) { $row[$i] = iconv('utf-8', 'gb2312//TRANSLIT//IGNORE', $v); } fputcsv($fp, $row); } } fclose($fp); return true; } throw new \yii\web\HttpException(500, "Not a valid parameter!"); }
iconv - The string is converted according to the required character encoding
Instructions
iconv ( string $in_charset , string $out_charset , string $str ) : string
Convert string str from in_charset to out_charset.
Parameters
in_charset: Input character set.
out_charset: Output character set.
str: ??The string to be converted.
Return value: Returns the converted string, or returns FALSE on failure.
Recommended learning: yii tutorial
The above is the detailed content of What to do if the csv file exported by yii2 is garbled?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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

Title: Methods and code examples to solve the problem of garbled characters when importing Chinese data into Oracle. When importing Chinese data into Oracle database, garbled characters often appear. This may be due to incorrect database character set settings or encoding conversion problems during the import process. . In order to solve this problem, we can take some methods to ensure that the imported Chinese data can be displayed correctly. The following are some solutions and specific code examples: 1. Check the database character set settings In the Oracle database, the character set settings are

How to deal with the problem of garbled characters in the Linux terminal. When using the Linux system, sometimes the text displayed in the terminal will be garbled. This brings inconvenience to us when using the terminal and needs to be dealt with in time. This article will introduce how to deal with some common Linux terminal garbled problems, and provide specific code examples. Problem 1: Garbled Chinese characters on the terminal. Garbled Chinese characters on the terminal are usually caused by incorrect character encoding settings on the terminal. We can solve this problem by modifying the terminal's character encoding settings. #View the current terminal

Tips for dealing with garbled Chinese file names in PHP During the development process, we often encounter the problem of garbled Chinese file names, especially when processing files uploaded by users. In PHP, how to correctly handle garbled file names is a common and important problem. This article will introduce some techniques for dealing with garbled Chinese file names and provide specific code examples to help readers better deal with this challenge. Problem description: When users upload files, the Chinese file names sometimes appear to be garbled. This is because different operating systems and browsers

Strategies and techniques for solving Chinese garbled characters in Oracle database. In actual database applications, many developers or administrators may encounter the problem of Chinese garbled characters in Oracle database. When the data in the database is garbled, it not only affects the correctness and readability of the data, but also brings many problems to the system. This article will combine specific code examples to introduce the strategies and techniques for solving Chinese garbled characters in Oracle database to help readers better understand and solve this problem. 1. The reason for garbled characters is inconsistent character sets: database connection and application

Win11 is Microsoft's latest operating system, but some users may encounter the problem of displaying garbled characters when booting, which will affect the normal use of the system. This article will introduce some methods to solve this problem. Method 1: 1. Press the [Win+S] key combination, or click the [Search icon] next to the start icon on the taskbar. In the opened Windows search, enter [Control Panel] in the search box, and then click [Open]. The best matching control panel application out of the control panel window; 2. In the control panel window, switch to the [Category] view mode, and then click [Clock and Zone-Region]; 3. In the zone window, switch to the [Management] tab, and then click [Change] System Regional Settings]; 4. [Uncheck] Beta version: Use Unicode

PHP is a commonly used server-side scripting language, often used to develop dynamic web pages. In the process of web development, we often encounter the problem of garbled Chinese characters, which is caused by inconsistent character encoding. This article will discuss in detail the reasons and solutions for Chinese garbled characters displayed on PHP web pages, and provide specific code examples. 1. Reason analysis: The encoding problem of the PHP file itself: The encoding of the PHP file should be consistent with the server-side environment. If the PHP file is saved in UTF-8 encoding and the server-side environment uses another encoding, it will

PHP is a back-end programming language widely used in website development. It has powerful database operation functions and is often used to interact with databases such as MySQL. However, due to the complexity of Chinese character encoding, problems often arise when dealing with Chinese garbled characters in the database. This article will introduce the skills and practices of PHP in handling Chinese garbled characters in databases, including common causes of garbled characters, solutions and specific code examples. Common reasons for garbled characters are incorrect database character set settings: the correct character set needs to be selected when creating the database, such as utf8 or u
