總結下現在我知道的php產生靜態頁面的辦法

來源:互聯網
上載者:User
首先說原理。某駝查了那麼多資料,發現不管用什麼方法,原理都是一樣的。就是用程式讀取相應的資料來替換模版中的變數,然後產生靜態頁。php中主要用到的就是要用到fread()和fwirte()。而靜態頁面產生了之後,就會牽扯到修改的問題。這裡可以用到正則匹配的方法來替換模版中改變的部位。不過此種方法太麻煩,駝駝推薦的方法是直接把原來產生的模版砍掉,重建,呵呵,真正的一了百了。 

  還需要說明的一點就是,這種產生靜態頁面的方法一般都用於那些變化不是很頻繁的頁面,比如資訊的最終頁面。而針對列表頁,如果資訊更新不是很頻繁的話,也是可取的。現在網上流行好多可以產生靜態頁面的blog或者論壇程式,都是通過手動點擊後台“產生html頁”的按鈕來“半自動”產生html的。而對一些資訊量非常大的門戶網站,則行不通。因為靜態頁之所以叫“靜態”,是因為其不可自動改變。如果資訊列表每天更新100次,那麼靜態列表頁就要重建100次。如果我有10個這樣的欄目,那想想也夠吐血的了。

 

  好了,閑話少說,現在來看看實際的程式示範:

  first:是一個利用ob函數來做的咚咚,代碼比較簡單,效率相對也高一些。某駝從某個

         高人處得到的源碼,做了一些改動

  <?php
    ob_start();
    @readfile("http://localhost/?package=pricab&place_port=4");
    $text = ob_get_flush();

    $myfile = fopen("myfile.html","w");

    $text  = str_replace ("{counent}",$string,$text);
    fwrite($myfile,$text);
    ob_clean();
  ?>

  

  因為就算要產生靜態頁面,動態讀取那部分也是要保留的,把資料插入資料庫後,把url傳遞給readfile函數,然後讀入緩衝,fwrite一下就可以產生靜態頁面,這個是駝駝最欣賞的一種作法。程式碼數最少,效率最高。駝駝這邊要求http://localhost/?package=pricab&place_port=4是一個裸頁,也就是單純的內容,沒有頭,尾,菜單。這樣才能比較自由的定製自己的模版myfile.html。如果僅僅是要求產生靜態頁的話,

    ob_start();
    @readfile("http://localhost/?package=pricab&place_port=4");
    $string = ob_get_flush();    $myfile = fopen("myfile.html","w");
    fwrite($myfile,$string);
    ob_clean();

    就可以over了

 

   second:普通產生靜態html頁。

   這種作法就是按部就班的來做,fread進來頁面,然後str_replace替換

   首先是建立最終內容頁:

   $title = "http://siyizhu.com測試模板";
   $file = "TwoMax Inter test templet,<br>author:Matrix@Two_Max";

   $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."成功!");

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

 

      <?php
    $title = "http://";
    $file = "TwoMax Inter test templet,<br>author:Matrix@Two_Max";
    $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 .= '<a href='.$root.$result['filename'].'   target=_blank>'.$result['title'].'</a><br>';
    }
    $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."成功!";這句去掉,放到迴圈後的顯示,因為該語句將中止程式執行。例: <?php
 $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 .= '<a href='.$root.$result['filename'].' target=_blank>'.$title.'</a><br>';
 }
 $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 ("產生分頁檔案完成,如產生不完全,請檢查檔案許可權系統後重建!");
?>

 

 

 

third:smarty模版產生靜態頁面

駝駝是用smarty模版的,smarty自己有一個fetch函數,其功用有點類似於fread()可以用來產生靜態頁面.

這個例子大家想必看起來眼熟,對,smarty手冊中關於fetch函數的例子,hoho  某駝借用一下,比竟官方的例子總是很經典的嘛!

<?php
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/2005/05/19/0001.html", "w");
fwrite($fp, $content);
fclose($fp);

?>

?>  

聯繫我們

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