php產生靜態html檔案的原理分析

來源:互聯網
上載者:User
  1. //引入資料庫設定檔

  2. include( dirname(dirname(__FILE__))."\include\config.php" );

  3. /**

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

  12. //將日期、時間變數分解成數組

  13. $GetDateRow = explode("-", $Date);
  14. $GetTimeRow = explode(":",$Time);

  15. //得到檔案的名字。比如:20121028210632.html

  16. $FileName = $GetDateRow[0].$GetDateRow[1].$GetDateRow[2].$GetTimeRow[0].$GetTimeRow[1].$GetTimeRow[2].".html";

  17. //開啟並讀模數板內容

  18. $FP = fopen("tmp.html","r");
  19. $Str = fread($FP,filesize("tmp.html"));

  20. //得到替換後的模板內容

  21. $Str = str_replace("{Title}",$Title, $Str);
  22. $Str = str_replace("{Content}", $Content, $Str);
  23. $Str = str_replace("{Name}", $Name, $Str);
  24. $Str = str_replace("{Date}", $Date,$Str);
  25. $Str = str_replace("{Time}", $Time, $Str);

  26. //關閉檔案,減少伺服器的壓力。

  27. fclose($FP);

  28. //將內容寫入HTML檔案

  29. $Handle = fopen($FileName,"w");
  30. fwrite($Handle,$Str);
  31. fclose($Handle);

  32. //測試

  33. //echo "ok,done!";
  34. }

  35. //資料庫的操作

  36. $querysql = "select * from article";
  37. $queryset = mysql_query($querysql);

  38. //迴圈產生HTML檔案。

  39. while( $row = mysql_fetch_array($queryset) ){
  40. GenerateHTML($row['date'],$row['time'],$row['content'],$row['title'],$row['name']);
  41. }
  42. ?>

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.