php 中文亂碼問題解決

来源:互联网 发布:战舰世界大和数据 编辑:程序博客网 时间:2024/05/09 15:19

1、PHP 編輯器zend_studio默認編碼方式是MS950,如果直接在編輯器文檔中出現中文漢字,需要將MS950改為UTF-8,這樣在瀏覽器中才不會亂碼。

2、從MYSQL查詢結果后,需要用phpexcel存為excel。數據庫的編碼方式要和phpexcel一致,否則中文會顯示為“?”或者“FALSE”.使用

     mb_convert_encoding($key1,"utf8","big5") 可以把數據庫的編碼方式“big5”轉換為phpexcel默認的utf8格式。只要在每個單元格填充

  $objPHPExcel->getActiveSheet()->setTitle(mb_convert_encoding($report_name,"utf8","big5"));
$report_name1=$report_name.'.xlsx';
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($report_name1);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$report_name2=str_replace('.xlsx', '.xls', $report_name1);
$objWriter->save($report_name2);

數據前,先進行轉換即可。

3、把生成的excel檔,作為郵件附件傳送。中文亂碼也很多

$mail = new Zend_Mail();
$mailcontent='附件為同一份報表的兩個不同excel版本(excel2003和excel2007)';
$mail->setBodyText($mailcontent,'utf-8');        //setBodyText函數,第二個參數默認為字符編碼方式。
$mail->createAttachment( file_get_contents($report_name1), 'text/plain',Zend_Mime::DISPOSITION_INLINE  , Zend_Mime::ENCODING_BASE64 , mb_convert_encoding($report_name1,"utf8","big5"));  //將繁體中文轉換為utf8后再作為附件信息傳送
$mail->createAttachment( file_get_contents($report_name2), 'text/plain',Zend_Mime::DISPOSITION_INLINE  , Zend_Mime::ENCODING_BASE64 , mb_convert_encoding($report_name2,"utf8","big5"));
$send_mail=explode(';', $send_man);
foreach ($send_mail as $send_str)
{ $mail->addTo($send_str);
}
$mail->setSubject("=?big5?B?".base64_encode($report_name)."?=");  //郵件標題亂碼,需要將本地郵件編碼方式固定寫入。此格式是JMAIL固定格式,如果本地郵件編碼方
$mail->send();                                                                                                      //為其他,只要替換文件中的big5就OK了

原创粉丝点击