利用PHP產生靜態HTML文檔的原理_php執行個體

來源:互聯網
上載者:User
給出代碼:
複製代碼 代碼如下:
//引入資料庫設定檔
include( dirname(dirname(__FILE__))."\include\config.php" );

/**
*
* 將資料庫中的文章產生單個HTML檔案.
* @param Date $Date
* @param Time $Time
* @param String $Content
* @param String $Title
*/
function GenerateHTML($Date,$Time,$Content,$Title,$Name){

//將日期、時間變數分解成數組
$GetDateRow = explode("-", $Date);
$GetTimeRow = explode(":",$Time);

//得到檔案的名字。比如:20121028210632.html
$FileName = $GetDateRow[0].$GetDateRow[1].$GetDateRow[2].$GetTimeRow[0].$GetTimeRow[1].$GetTimeRow[2].".html";

//開啟並讀模數板內容
$FP = fopen("tmp.html","r");
$Str = fread($FP,filesize("tmp.html"));

//得到替換後的模板內容
$Str = str_replace("{Title}",$Title, $Str);
$Str = str_replace("{Content}", $Content, $Str);
$Str = str_replace("{Name}", $Name, $Str);
$Str = str_replace("{Date}", $Date,$Str);
$Str = str_replace("{Time}", $Time, $Str);

//關閉檔案,減少伺服器的壓力。
fclose($FP);

//將內容寫入HTML檔案
$Handle = fopen($FileName,"w");
fwrite($Handle,$Str);
fclose($Handle);

//小測一下
//echo "ok,done!";

}

//資料庫的操作
$querysql = "select * from article";
$queryset = mysql_query($querysql);

//迴圈產生HTML檔案。
while( $row = mysql_fetch_array($queryset) ){
GenerateHTML($row['date'],$row['time'],$row['content'],$row['title'],$row['name']);
}
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.