首頁  >  文章  >  後端開發  >  php產生靜態頁面的三種方法與程式碼詳解

php產生靜態頁面的三種方法與程式碼詳解

WBOY
WBOY原創
2016-07-25 09:05:48930瀏覽
  1. ob_start();
  2. @readfile("http://bbs.it-home.org/");
  3. $text = ob_get_flush();
  4. $myfile = fopen("myfile.html","w");
  5. $text =
  6. str_replace ("{counent}",$string,$text);
  7. fwrite($myfile,$text);
  8. ob_clean();
  9. ?>
複製程式碼

因為就算要產生靜態頁,動態讀取那部分也是要保留的,把資料插入資料庫後,把url傳遞給readfile函數,然後讀入緩存,fwrite一下就可以產生靜態頁面,這個是駝駝最欣賞的一種作法。程式碼行數最少,效率最高。 http://bbs.it-home.org/是一個裸頁,也就是單純的內容,沒有頭,尾,選單。這樣才能比較自由的自訂自己的模版myfile.html。如果只是要求產生靜態頁的話,這樣基本上就滿足需求了。

二、普通產生靜態html頁 fread進來頁面,然後st​​r_replace替換 首先,建立最終內容頁:

  1. $title = "http://siyizhu.com測試模板";
  2. $file = "TwoMax Inter test templet ,
    author:[email=Matrix@Two_Max]Matrix@Two_Max[/email]";
  3. $fp = fopen ("temp.html","r");
  4. $content = fread($ fp,filesize ("temp.html"));
  5. $content = str_replace("{file}",$file,$content);
  6. $content = str_replace("{title}",$title, $content);
  7. $filename = "test/test.html";
  8. $handle = fopen ($filename,"w"); //開啟檔案指針,建立檔案
  9. /*  檢查檔案是否被建立且可寫*/
  10. if (!is_writable ($filename))
  11. {
  12. die ("檔案:".$filename."不可寫,請檢查其屬性後再試一次!") ;
  13. }
  14. if (!fwrite ($handle,$content))
  15. { //將資訊寫入檔案
  16. die ("產生檔案".$filename."失敗!");
  17. }
  18. fclose ($handle); //關閉指標
  19. die ("建立檔案".$filename."成功!");
  20. ?>
複製程式碼

這一步驟只是單純的變數替換即可。 如果要產生靜態的列表頁面的話,原理也是一樣,用程式來產生文章列表,把它當成一個大的變量,替換模版中的變量,列表的翻頁頁是如此。 當然,如果有資訊更新的話,清單翻頁也是要重新產生的。

  1. $title = "http://";
  2. $file = "TwoMax Inter test templet,
    author :[email=Matrix@Two_Max]Matrix@Two_Max[/email]";
  3. $fp = fopen ("temp.html","r");
  4. $content = fread ($fp,filesize (" temp.html"));
  5. $content = str_replace ("{file}",$file,$content);
  6. $content = str_replace ("{title}",$title,$content);
  7. // 產生清單開始
  8. $list = '';
  9. $sql = "select id,title,filename from article";
  10. $query = mysql_query ($sql);
  11. while( $result = mysql_fetch_array ($query))
  12. {
  13. $list .= ''.$result['title '].'
    ';
  14. }
  15. $content .= str_replace("{articletable}",$list,$content);//產生清單結束
  16. // echo $content;
  17. $filename = "test/test.html";
  18. $handle = fopen ($filename,"w");
  19. //開啟檔案指針,建立檔案
  20. /* 檢查檔案是否已建立且可寫入*/
  21. if(!is_writable ($filename))
  22. {
  23. die ("檔案:".$filename."不可寫,請檢查其屬性後再試! ");
  24. }
  25. if(!fwrite($handle,$content))
  26. { //將資訊寫入檔案
  27. die ("產生檔案".$filename."失敗!" );
  28. }
  29. fclose($handle); //關掉指標
  30. die ("建立檔案".$filename."成功!");
  31. ?>
複製程式碼

關於翻頁: 如我們指定分頁時,每頁20篇。某子頻道清單內文章經資料庫查詢為45條,則,首先我們透過查詢得到以下參數:1,總頁數;2,每頁篇數。第二步,for ($i = 0; $i

  1. $fp = fopen ("temp.html","r ");
  2. $content = fread ($fp,filesize ("temp.html"));
  3. $onepage = '20';
  4. $sql = "select id from article where channel='$ channelid'";
  5. $query = mysql_query ($sql);
  6. $num = mysql_num_rows ($query);
  7. $allpages = ceil ($num / $onepage);
  8. for ($i = 0;$i{
  9. if ($i == 0)
  10. {
  11. $indexpath = "index.html";
  12. }
  13. else
  14. {
  15. $indexpath = "index_".$i."html";
  16. }
  17. $start = $i * $onepage;
  18. $list = '';
  19. $sql_for_page = "select name,filename,title from article where channel='$channelid' limit $start,$onepage";
  20. $query_for_page = mysql_query ($sql_for_page);
  21. whique
  22. {
  23. $list .= ''.$title.'
    ';
  24. }
  25. $content = str_replace("{articletable}",$list,$content);
  26. if (is_file ($indexpath))
  27. {
  28. @unlink ($indexpath); / /若檔案已存在,則刪除
  29. }
  30. $handle = fopen ($indexpath,"w"); //開啟檔案指針,建立檔案
  31. /*檢查檔案是否已建立且可寫入* /
  32. if (!is_writable ($indexpath))
  33. {
  34. echo "檔案:".$indexpath."不可寫,請檢查其屬性後再試! "; //修改為echo
  35. }
  36. if (!fwrite ($handle,$content))
  37. {//將資訊寫入檔案
  38. echo "產生檔案".$indexpath."失敗! "; //修改為echo
  39. }
  40. fclose ($handle); //關閉指標
  41. }
  42. fclose ($fp);
  43. die ("生成分頁檔案完成,如生成不完全,請檢查檔案權限系統後重新產生!產生靜態頁面
smarty自己有一個fetch函數,其功用有點類似fread()可以用來產生靜態的頁面。 有關smarty技術,大家可以看看這裡的幾篇文章: 1)、有關smarty的基本設置 2)、有關smarty快取的應用 3)、smarty產生靜態頁面的方法 4)、php模板引擎Smarty詳細介紹

include("Smarty.class.php");$smarty = new Smarty;$smarty->caching = true;

// only do db calls if cache doesn't exist
if(!$smarty->is_cached("index.tpl")){// dummy up some data
    $address = "245 N 50th";
  1. $db_data = array("City" => "Lincoln", "State" => "Nebraska", "Zip" = > "68502");
  2. $smarty->assign("Name","Fred");
  3. $smarty->assign("Address",$address);
  4. $smarty->assign( $db_data);
  5. }// capture the output
  6. $output = $smarty->fetch("index.tpl");
  7. //這個地方算是關鍵// do something with $output here
  8. echo $output; //hoho 看到output的結果了吧然後呢?fwrite一下,我們就得到我們所要的結果了。
  9. $fp = fopen("archives/2013/05/19/0001.html", "w");
  10. fwrite($fp, $content);
  11. fclose($fp);
  12. ?>
  13. 複製程式碼
  14. 複製程式碼
  15. 程式碼如下:
ob_start();echo "Hello World!";

$content = ob_get_content =echo "Hello World!";

$content = ob_get ;//取得php頁面輸出的全部內容
$fp = fopen("archives/2013/05/19/0001.html", "w");
fwrite($fp, $content);
    fclose($fp);
  1. ?>
  2. 複製程式碼
  3. 附註: 可以產生靜態頁面的blog或論壇程序,都是透過手動點擊後台「產生html頁」的按鈕來「半自動」產生html的。

    您可能感興趣的文章: php產生靜態頁函數(php2html)的範例 php產生靜態頁面的方法(三個函數) php產生html靜態頁面的方法參考 php寫的一個產生靜態頁面的類別 將資料庫中的所有內容產生html靜態頁面的程式碼 虛擬主機上定時自動產生靜態頁面的方法 php產生靜態頁面的詳細教學 apache中存取不了偽靜態頁面的解決方法 php寫的關於靜態頁面的蜘蛛爬行記錄的程式碼 smarty產生靜態頁面的方法 PHP產生靜態頁面的方法 apache存取不了偽靜態頁面的解決方法



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn