PHP Multi-language (UTF-8 encoding) export of Excel, CSV garbled solution Export UTF-8 encoded Excel, CSV

Source: Internet
Author: User

Transferred from: https://www.cnblogs.com/kclteam/p/5278926.html

The new project, presumably the situation is this: there may be many countries, different language users, the analogy of Chinese, traditional Chinese, Korean, Japanese and so on, the development of the choice of UTF-8 coding, development smooth, no problem. Yesterday did a CSV export function, the export of something completely chaotic:

Set mb_convert_encoding ($content, "gb2312", "UTF-8") when Chinese is normal

Set mb_convert_encoding ($content, "Shift-jis", "UTF-8") when Japanese is normal

When setting mb_convert_encoding ($content, "UTF-8", "UTF-8"), it's not normal.

Google for a reason, I understand that the general meaning is that Microsoft's CSV does not support UFT-8 encoding, but support utf-16le encoding, so do the following settings

1 // Output BOM 2 Echo (chr(255). CHR (254)); 3 Echo (Mb_convert_encoding ($content, "Utf-16le", "UTF-8"));

Normal display in various languages

The following is the full function, support double-byte file name (such as Japanese or Chinese file name) is not garbled

1<?PHP2 /**3 * Export to CSV file4 * @param $data export Arrays5 * @param $file _name file name6  */7 functionExport_csv ($data,$file _name= ' ')8 {9 Ten     $file _name=$file _name.‘ _‘.Date(' Ymdhi '). csv; One     $encoded _filename=UrlEncode($file _name); A         $encoded _filename=Str_replace("+", "%20",$encoded _filename ); -     $content= Array_to_string ($data); -     Header(' Cache-control:private '); the     //Judging Browser, output double-byte file name is not garbled -     $ua=$_server["Http_user_agent"]; -     if(Preg_match("/msie/",$ua)) { -         Header(' Content-disposition:attachment; Filename= '.$encoded _filename. ‘"‘); +     } -     Else if(Preg_match("/firefox/",$ua)) { +         Header(' content-disposition:attachment; filename*= ' utf8\ '.$file _name. ‘"‘); A     } at     Else { -         Header(' Content-disposition:attachment; Filename= '.$file _name. ‘"‘); -     } -     if(function_exists(' mb_convert_encoding ')){ -         Header(' Content-type:text/csv; Charset=utf-16le '); -         //Output BOM in         Echo(CHR(255).CHR(254)); -         Echo(Mb_convert_encoding ($content, "Utf-16le", "UTF-8")); to         Exit; +     } - } the /** * * Export Data Conversion $ * @param $resultPanax Notoginseng  */ - functionArray_to_string ($result) the { +     if(Empty($result)){ A         returnI ("No data to meet your requirements!") ^_^"); the     } +     $size _result=Count($result); -      for($i= 0;$i<$size _result;$i++) { $         $data.=$result[$i]." \ n "; $     } -     return $data; - } the?>

PHP Multi-language (UTF-8 encoding) export of Excel, CSV garbled solution Export UTF-8 encoded Excel, CSV

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.