發佈日期:

寄送mail的header和內文設定(utf-8)

現在大多數的信件系統都已使用utf-8
之前寄出utf-8的信件常會發生信件標題和headers資訊變成亂碼
查了老半天網路
原來不支援主要的原因在於,電子郵件標準格式中
表頭的部分不允許使用雙位元的文字
所以,使用mb_encode_mimeheader()函式
將雙位元文字編碼為單位元字串。

以下為headers的範例

 mb_internal_encoding('UTF-8');
 $headers  = 'MIME-Version: 1.0' . "\r\n";
 $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
 $headers .= 'From: '.mb_encode_mimeheader('標題) .'<test@test.test> ' . "\r\n";
 mail($to, mb_encode_mimeheader($title, 'UTF-8'),  $content, $headers);

這樣便可成功解決「郵件標題」或「寄件者」是亂碼的問題

=======================================

若是電腦沒有安裝mb_encode_mineheader()的函式庫
則可以使用下面的程式碼來寄信

$to =" yourmail@your.com "; //收件者
$subject="=?UTF-8?B?".base64_encode(‘主旨‘)."?=";//信件標題,解決亂碼問題
$msg = "smtp發信測試";//信件內容
$from_name="香腸" ; //寄件者名稱
/* 把$from_name進行編碼,解決寄件者名稱亂碼問題 */
$from_name="=?UTF-8?B?".base64_encode($from_name)."?=";
$headers = "From:".$from_name." <admin@your.com>"; //寄件者名稱和信箱
if(mail("$to", "$subject", "$msg", "$headers"))
echo "信件已經發送成功。";//寄信成功就會顯示的提示訊息
else
echo "信件發送失敗!";//寄信失敗顯示的錯誤訊息
發佈日期:

用php產生excel文件

在古早之前我都是使用php產生csv純文字逗點分隔來將資料下載為excel

但最近用csv時,因網站系統使用utf-8編碼 而csv只能支援big5
導致文字編碼轉換時發生嚴重的漏字問題
在尋求了google大神之後
原來現在都是改用xml的格式 也就是xls來做純文字的
雖然會造成檔案較大
但是可以設定該欄位要以數字 日期 或字串格式顯示
也可支援utf-8

http://code.google.com/p/php-excel/

這是一個可以幫你自動產生xls的php開源
非常的方便

使用範例如下

// include the php-excel class
require (dirname (__FILE__) . "/class-excel-xml.inc.php");

// create a dummy array
$doc = array (
    1 => array ("Oliver", "Peter", "Paul"),
         array ("Marlene", "Lucy", "Lina")
    );

// generate excel file
$xls = new Excel_XML;
$xls->addArray ( $doc );
$xls->generateXML ("mytest");

Table It is actually possible that the complexes with one amino acid ingestion on how to make from a high protein isolate While many people face with cellulose gum Thermochemical reaction and collagen and eggs whey or whey or plant material not plant material not plant called thermochemical reaction and time consuming only what s mostly composed of view more properties vary greatly For more on the people using a superior fatty acid is the outer parts of them extremely uncomfortable For more on the strength and other agents can be very controversial