$str = 28; echo sprintf("%06d",$str);
輸出結果為:000028
sprintf 說明
<*meta http-equiv=Content-Type content="text/html; charset=big5">改為:
<*meta http-equiv=Content-Type content="text/html; charset=utf-8">並存檔。
//讀入HEAD $fd = fopen("Excel/forT_head.txt", "r"); while (!feof($fd)) { $buffer = fgets($fd, 4096); $m_strHead.=$buffer; } //讀入BODY $fd = fopen("Excel/forT_body.txt", "r"); while (!feof($fd)) { $buffer = fgets($fd, 4096); $m_strBody.=$buffer; } //讀入FOOT $fd = fopen("Excel/forT_foot.txt", "r"); while (!feof($fd)) { $buffer = fgets($fd, 4096); $m_strFoot.=$buffer; } //示範使用迴圈 for ($i = 1; $i < 3; $i++) { //將body資料存入temp資料 $m_strBodytemp = $m_strBody; //取代temp資料的{name}字串 $m_strBodytemp = str_replace("{name}", "哎唷威".$i, $m_strBodytemp); //取代temp資料的{sex}字串 $m_strBodytemp = str_replace("{sex}", "男".$i, $m_strBodytemp); //取代temp資料的{address}字串 $m_strBodytemp = str_replace("{address}", "wei6028.blogspot.com".$i, $m_strBodytemp); //取代temp資料的{tel}字串 $m_strBodytemp = str_replace("{tel}", "091234567".$i, $m_strBodytemp); //再存入$m_strBodyMix $m_strBodyMix .= $m_strBodytemp; } //最後利用header方式匯出Excel $m_strFilename = sprintf("excel_teach_%s.xls", date("YmdHis")); header("Content-type:application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=" . $m_strFilename); //印出Head BodyMix Foot echo $m_strHead . $m_strBodyMix . $m_strFoot;最後,匯出來的資料,
if (strpos($_SERVER["HTTP_USER_AGENT"], "MSIE 8.0")) echo "Internet Explorer 8.0"; else if (strpos($_SERVER["HTTP_USER_AGENT"], "MSIE 7.0")) echo "Internet Explorer 7.0"; else if (strpos($_SERVER["HTTP_USER_AGENT"], "MSIE 6.0")) echo "Internet Explorer 6.0"; else if (strpos($_SERVER["HTTP_USER_AGENT"], "Firefox/3")) echo "Firefox 3"; else if (strpos($_SERVER["HTTP_USER_AGENT"], "Firefox/2")) echo "Firefox 2"; else if (strpos($_SERVER["HTTP_USER_AGENT"], "Chrome")) echo "Google Chrome"; else if (strpos($_SERVER["HTTP_USER_AGENT"], "Safari")) echo "Safari"; else if (strpos($_SERVER["HTTP_USER_AGENT"], "Opera")) echo "Opera"; else echo $_SERVER["HTTP_USER_AGENT"];
$str = "HelloWorld"; echo substr($str,3); 執行結果: loWorld
$str = "HelloWorld"; print_r (str_split($str)); 執行結果: Array ( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => W [6] => o [7] => r [8] => l [9] => d )
function utf8_str_split($str, $split_len = 1) { if (!preg_match('/^[0-9]+$/', $split_len) || $split_len < 1) { return FALSE; } $len = strlen($str); if ($len <= $split_len) { return array($str); } preg_match_all('/.{' . $split_len . '}|[^\x00]{1,' . $split_len . '}$/us', $str, $ar); return $ar[0]; }簡單解釋是利用正規式加上strlen達成切割的功能。 實際使用結果:
$str = "Hello哈囉"; print_r (utf8_str_split($str)); 執行結果: Array ( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => 哈 [6] => 囉 ) print_r (utf8_str_split($str,3)); 執行結果: Array ( [0] => Hel [1] => lo哈 [2] => 囉 )