- ob_start();
- @readfile("http://bbs.it-home.org/");
- $text = ob_get_flush();
- $myfile = fopen("myfile.html","w");
- $text =
- str_replace ("{counent}",$string,$text);
- fwrite($myfile,$text);
- ob_clean();
- ?>
複製代碼因為就算要產生靜態頁面,動態讀取那部分也是要保留的,把資料插入資料庫後,把url傳遞給readfile函數,然後讀入緩衝,fwrite一下就可以產生靜態頁面,這個是駝駝最欣賞的一種作法。程式碼數最少,效率最高。http://bbs.it-home.org/是一個裸頁,也就是單純的內容,沒有頭,尾,菜單。這樣才能比較自由的定製自己的模版myfile.html。如果僅僅是要求產生靜態頁的話,這樣基本上就滿足需求了。 二、普通產生靜態html頁fread進來頁面,然後str_replace替換首先,建立最終內容頁:
- $title = "http://siyizhu.com測試模板";
- $file = "TwoMax Inter test templet,
author:[email=Matrix@Two_Max]Matrix@Two_Max[/email]";
- $fp = fopen ("temp.html","r");
- $content = fread($fp,filesize ("temp.html"));
- $content = str_replace("{file}",$file,$content);
- $content = str_replace("{title}",$title,$content);
- $filename = "test/test.html";
- $handle = fopen ($filename,"w"); //開啟檔案指標,建立檔案
- /* 檢查檔案是否被建立且可寫 */
- if (!is_writable ($filename))
- {
- die ("檔案:".$filename."不可寫,請檢查其屬性後重試!");
- }
- if (!fwrite ($handle,$content))
- { //將資訊寫入檔案
- die ("組建檔案".$filename."失敗!");
- }
- fclose ($handle); //關閉指標
- die ("建立檔案".$filename."成功!");
- ?>
複製代碼這一步只是單純的變數替換即可。如果要產生靜態列表頁面的話,原理也是一樣,用程式來產生文章列表,把它當成一個大的變數,替換模版中的變數,列表的翻頁頁是如此。當然,如果有資訊更新的話,列表翻頁也是要重建的。
- $title = "http://";
- $file = "TwoMax Inter test templet,
author:[email=Matrix@Two_Max]Matrix@Two_Max[/email]";
- $fp = fopen ("temp.html","r");
- $content = fread ($fp,filesize ("temp.html"));
- $content = str_replace ("{file}",$file,$content);
- $content = str_replace ("{title}",$title,$content);
- // 產生列表開始
- $list = '';
- $sql = "select id,title,filename from article";
- $query = mysql_query ($sql);
- while($result = mysql_fetch_array ($query))
- {
- $list .= ''.$result['title'].'
';
- }
- $content .= str_replace("{articletable}",$list,$content);//產生列表結束
- // echo $content;
- $filename = "test/test.html";
- $handle = fopen ($filename,"w");
- //開啟檔案指標,建立檔案
- /* 檢查檔案是否被建立且可寫 */
- if(!is_writable ($filename))
- {
- die ("檔案:".$filename."不可寫,請檢查其屬性後重試!");
- }
- if(!fwrite($handle,$content))
- { //將資訊寫入檔案
- die ("組建檔案".$filename."失敗!");
- }
- fclose($handle); //關閉指標
- die ("建立檔案".$filename."成功!");
- ?>
複製代碼關於翻頁:如我們指定分頁時,每頁20篇。某子頻道列表內文章經資料庫查詢為45條,則,首先我們通過查詢得到如下參數:1,總頁數;2,每頁篇數。第二步,for ($i = 0; $i < allpages; $i++),頁面元素擷取,分析,文章產生,都在此迴圈中執行。不同的是,die ("建立檔案".$filename."成功!";這句去掉,放到迴圈後的顯示,因為該語句將中止程式執行。例:
- $fp = fopen ("temp.html","r");
- $content = fread ($fp,filesize ("temp.html"));
- $onepage = '20';
- $sql = "select id from article where channel='$channelid'";
- $query = mysql_query ($sql);
- $num = mysql_num_rows ($query);
- $allpages = ceil ($num / $onepage);
- for ($i = 0;$i<$allpages; $i++)
- {
- if ($i == 0)
- {
- $indexpath = "index.html";
- }
- else
- {
- $indexpath = "index_".$i."html";
- }
- $start = $i * $onepage;
- $list = '';
- $sql_for_page = "select name,filename,title from article where channel='$channelid' limit $start,$onepage";
- $query_for_page = mysql_query ($sql_for_page);
- while ($result = $query_for_page)
- {
- $list .= ''.$title.'
';
- }
- $content = str_replace("{articletable}",$list,$content);
- if (is_file ($indexpath))
- {
- @unlink ($indexpath); //若檔案已存在,則刪除
- }
- $handle = fopen ($indexpath,"w"); //開啟檔案指標,建立檔案
- /*檢查檔案是否被建立且可寫 */
- if (!is_writable ($indexpath))
- {
- echo "檔案:".$indexpath."不可寫,請檢查其屬性後重試!"; //修改為echo
- }
- if (!fwrite ($handle,$content))
- {//將資訊寫入檔案
- echo "組建檔案".$indexpath."失敗!"; //修改為echo
- }
- fclose ($handle); //關閉指標
- }
- fclose ($fp);
- die ("產生分頁檔案完成,如產生不完全,請檢查檔案許可權系統後重建!");
- ?>
複製代碼三、smarty模版產生靜態頁面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";
- $db_data = array("City" => "Lincoln", "State" => "Nebraska", "Zip" => "68502");
- $smarty->assign("Name","Fred");
- $smarty->assign("Address",$address);
- $smarty->assign($db_data);
- }// capture the output
- $output = $smarty->fetch("index.tpl");
- //這個地方算是關鍵// do something with $output here
- echo $output; //hoho 看到output的結果了吧 然後呢?fwrite一下,我們就得到我們所要的結果了。
- $fp = fopen("archives/2013/05/19/0001.html", "w");
- fwrite($fp, $content);
- fclose($fp);
- ?>
複製代碼複製代碼 代碼如下:
- ob_start();
- echo "Hello World!";
- $content = ob_get_contents();//取得php頁面輸出的全部內容
- $fp = fopen("archives/2013/05/19/0001.html", "w");
- fwrite($fp, $content);
- fclose($fp);
- ?>
-
複製代碼附註:可以產生靜態頁面的blog或者論壇程式,都是通過手動點擊後台“產生html頁”的按鈕來“半自動”產生html的。 您可能感興趣的文章:php產生靜態頁面函數(php2html)的例子php產生靜態頁面的方法(三個函數)php產生html靜態頁面的方法參考php寫的一個產生靜態頁面的類將資料庫中的所有內容產生html靜態頁面的代碼虛擬機器主機上定時自動產生靜態頁面的方法php產生靜態頁面的詳細教程apache中訪問不了偽靜態頁面的解決方案php寫的關於靜態頁面的蜘蛛爬行記錄的代碼smarty產生靜態頁面的方法PHP產生靜態頁面的方法apache訪問不了偽靜態頁面的解決方案 |