Home >Backend Development >PHP Tutorial >PHP and email are sent with csv attachments. After downloading and opening, the content text is garbled. Please solve it.
Result from inconsistent encoding formats on both sides.
<code>//CSV的Excel支持GBK编码,一定要转换,否则乱码 $content = iconv('utf-8', 'gbk//ignore', $content); </code>
Two-dimensional array:
<code>foreach($dataArr as $key => $value){ foreach ($value as $k1 => $v1){ $value[$k1] = iconv('utf-8', 'gbk//ignore', $v1); } fputcsv($fh, $value); } fclose($fh);</code>
It should be an encoding problem.
<code>//对表头进行转码: $header_data = ['第一列','第二列']; echo iconv('utf-8','gbk//TRANSLIT','"'.implode('","',$header_data).'"'."\n"); </code>
Try it~
For specific methods, please refer to: PHP reading/exporting CSV files
First confirm, is the encoding of the content of the csv file you generated or the csv file you sent before normal?
The email said it was my fault