日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

解析php生成靜態(tài)頁面的辦法_PHP教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:淺談微軟對PHP支持的改進 及其它一些胡言亂語
這里有一份我覺得值得推薦給大家的幻燈片(下載鏈接)。這個幻燈片是不久前舉辦的WordCamp China 2009上微軟王超群的演講,演講主題是《熟悉的陌生人:微軟對PHP的新支持使WordPress在IIS7上雄起》。WordCamp是業(yè)界著名的PHP大會,不過我這里推薦這個幻燈片

首先說原理。某駝查了那么多資料,發(fā)現(xiàn)不管用什么方法,原理都是一樣的。就是用程序讀取相應(yīng)的數(shù)據(jù)來替換模版中的變量,然后生成靜態(tài)頁。php中主要用到的就是要用到fread()和fwirte()。而靜態(tài)頁面生成了之后,就會牽扯到修改的問題。這里可以用到正則匹配的方法來替換模版中改變的部位。不過此種方法太麻煩,駝駝推薦的方法是直接把原來生成的模版砍掉,重新生成,呵呵,真正的一了百了。

還需要說明的一點就是,這種生成靜態(tài)頁面的方法一般都用于那些變化不是很頻繁的頁面,比如信息的最終頁面。而針對列表頁,如果信息更新不是很頻繁的話,也是可取的�,F(xiàn)在網(wǎng)上流行好多可以生成靜態(tài)頁面的blog或者論壇程序,都是通過手動點擊后臺“生成html頁”的按鈕來“半自動”生成html的。而對一些信息量非常大的門戶網(wǎng)站,則行不通。因為靜態(tài)頁之所以叫“靜態(tài)”,是因為其不可自動改變。如果信息列表每天更新100次,那么靜態(tài)的列表頁就要重新生成100次。如果我有10個這樣的欄目,那想想也夠吐血的了。

好了,閑話少說,現(xiàn)在來看看實際的程序演示:

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

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

<?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();
?>

因為就算要生成靜態(tài)頁面,動態(tài)讀取那部分也是要保留的,把數(shù)據(jù)插入數(shù)據(jù)庫后,把url傳遞給readfile函數(shù),然后讀入緩存,fwrite一下就可以生成靜態(tài)頁面,這個是駝駝最欣賞的一種作法。代碼行數(shù)最少,效率最高。駝駝這邊要求http://localhost/?package=pricab&place_port=4是一個裸頁,也就是單純的內(nèi)容,沒有頭,尾,菜單。這樣才能比較自由的定制自己的模版myfile.html。如果僅僅是要求生成靜態(tài)頁的話,
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:普通生成靜態(tài)html頁。

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

首先是創(chuàng)建最終內(nèi)容頁:

$title = "http://siyizhu.com測試模板";  
$file = "TwoMax Inter test templet,<br>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"); //打開文件指針,創(chuàng)建文件   /*  檢查文件是否被創(chuàng)建且可寫   */   if (!is_writable ($filename))
{     die ("文件:".$filename."不可寫,請檢查其屬性后重試!");   }  
if (!fwrite ($handle,$content)){ //將信息寫入文件     die ("生成文件".$filename."失敗!");   }    
fclose ($handle); //關(guān)閉指針    die ("創(chuàng)建文件".$filename."成功!");

這一步比較簡單。只是單純的變量替換即可。如果要生成靜態(tài)的列表頁面的話,原理也是一樣,用程序來生成文章列表,把它當(dāng)成一個大的變量,替換模版中的變量,列表的翻頁頁是如此。當(dāng)然,如果有信息更新的話,列表翻頁也是要重新生成的。

<?php    
$title = "http://";    
$file = "TwoMax Inter test templet,<br>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 .= '<a href='.$root.$result['filename'].'   target=_blank>'.$result['title'].'</a><br>';    }    
$content .= str_replace ("{articletable}",$list,$content);     //生成列表結(jié)束  
// echo $content;    
$filename = "test/test.html";    
$handle = fopen ($filename,"w"); //打開文件指針,創(chuàng)建文件  
/*  檢查文件是否被創(chuàng)建且可寫    */    if (!is_writable ($filename)){     die ("文件:".$filename."不可寫,請檢查其屬性后重試!");    }    if (!fwrite ($handle,$content)){ //將信息寫入文件     die ("生成文件".$filename."失��!");    }     fclose ($handle); //關(guān)閉指針     die ("創(chuàng)建文件".$filename."成功!"); ?>

關(guān)于翻頁:
 
如我們指定分頁時,每頁20篇。某子頻道列表內(nèi)文章經(jīng)數(shù)據(jù)庫查詢?yōu)?5條,則,首先我們通過查詢得到如下參數(shù):1,總頁數(shù);2,每頁篇數(shù)。第二步,for ($i = 0; $i < allpages; $i++),頁面元素獲取,分析,文章生成,都在此循環(huán)中執(zhí)行。不同的是,die ("創(chuàng)建文件".$filename."成功!";這句去掉,放到循環(huán)后的顯示,因為該語句將中止程序執(zhí)行。例:

<?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"); //打開文件指針,創(chuàng)建文件 /*  檢查文件是否被創(chuàng)建且可寫 */ if (!is_writable ($indexpath)){ echo "文件:".$indexpath."不可寫,請檢查其屬性后重試!"; //修改為echo } if (!fwrite ($handle,$content)){ //將信息寫入文件 echo "生成文件".$indexpath."失敗!"; //修改為echo } fclose ($handle); //關(guān)閉指針 }
fclose ($fp); die ("生成分頁文件完成,如生成不完全,請檢查文件權(quán)限系統(tǒng)后重新生成!");?>
third:smarty模版生成靜態(tài)頁面
駝駝是用smarty模版的,smarty自己有一個fetch函數(shù),其功用有點類似于fread()可以用來生成靜態(tài)的頁面.
這個例子大家想必看起來眼熟,對,smarty手冊中關(guān)于fetch函數(shù)的例子,hoho 某駝借用一下,比竟官方的例子總是很經(jīng)典的嘛!
<?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");
//這個地方算是關(guān)鍵// do something with $output here echo $output; //hoho
看到output的結(jié)果了吧
然后呢?fwrite一下,我們就得到我們所要的結(jié)果了。
$fp = fopen("archives/2005/05/19/0001.html", "w");
fwrite($fp, $content);fclose($fp);
?>
<?phpob_start();echo "Hello World!";
$content = ob_get_contents();//取得php頁面輸出的全部內(nèi)容
$fp = fopen("archives/2005/05/19/0001.html", "w");
fwrite($fp, $content);
fclose($fp);
?>
PHP生成靜態(tài)頁面類
/*********************/
/*                   */
/* Version : 5.2.5 */
/* Author : liqiangwork#sohu.com */
/* QQ : 570937581   */
/*                   */
/*********************/
//-----------------------------生成靜態(tài)的類-------------------------------
class Makehtml{
   public $MbUrl,$OutUrl,$AllHtml,$SouChar,$ObjChar; //變量
   public $row; //游標(biāo)
   public $Shuzusou,$Shuzuobj; //替換的字符串?dāng)?shù)組
   //-----------------------初始化-------------------------
    function __construct(){ //初始化
     $this->MbUrl="";
     $this->OutUrl="";
     $this->AllHtml="";
     $this->Sql="";
     $this->SouChar="";
     $this->ObjChar="";
    }
  
  
   //-----------------------------自動按字段替換---------------------------
    function AutoReplace(){
      //------------------自動獲取要替換的字符串-------------------
      $tlen=count($row);
      $shuzu1=array();
      $shuzu2=array();
      if($row){
      $i=0;
      foreach($row as $key => $value){      
     $shuzu2="<=\$".$key."\$>";
     $shuzu1=$value;
     $i++;
      }
      $this->Replacehtml(shuzu2,shuzu1);
      }
      //------------------自動獲取要替換的字符串-------------------
    }
   //-----------------------------自動按字段替換完成------------------------
  
  
   //-----------------------------批量替換數(shù)組--------------------------
   function Replacehtml($Shuzusou,$Shuzuobj){ //批量替換數(shù)組
    if(count($Shuzusou)!=count($Shuzuobj)){
      exit("替換數(shù)組不匹配");
    }
    if($this->AllHtml==""){
      exit("沒有要替換的內(nèi)容");    
    }
    for($i=0;$i<count($Shuzusou);$i++){
     $this->AllHtml=str_replace($Shuzusou[$i],$Shuzuobj[$i],$this->AllHtml);
     //print("<br>".$Shuzusou(i)."=".$Shuzuobj(i)."<br>")
   }
   }
   //-----------------------------批量替換數(shù)組完成--------------------------
  
   //-----------------------------讀取文件---------------------------------
    function Readfile(){
     $file=fopen($this->MbUrl,"r");
     $fsize=filesize($this->MbUrl);
     $this->AllHtml=fread($file,$fsize);
     fclose($file);
       }  
   //-----------------------------讀取文件完成------------------------------
   //-----------------------------保存文件---------------------------------
   function SaveFile(){
    $file=fopen($this->OutUrl,"w");
    fwrite($file,$this->AllHtml);
    fclose($file);
   }
   //-----------------------------保存文件完成------------------------------
}
//------------------------------生成靜態(tài)的類完成-------------------------------

以下為引用的內(nèi)容:
//------------------靜態(tài)生成----
     $MyMake=new Makehtml;
     $MyMake->MbUrl="News_Show.shtml";
     $MyMake->Readfile();
     $THTml=$MyMake->AllHtml;
     $shuzu1=array();
     $shuzu2=array();
     $shuzu1[0]="<=\$keybord\$>";
     $shuzu1[1]="<=\$description\$>";
     $shuzu1[2]="<=\$title\$>";
     $shuzu1[3]="<=\$Title1\$>";
     $shuzu1[4]="<\$=Bid\$>";
     $shuzu1[5]="<\$=Id\$>";
     $shuzu1[6]="<=\$Contentb\$>";
     $shuzu1[7]="<\$=BigId\$>";
     $shuzu1[8]="<=\$Date\$>";
     $shuzu1[9]="<=\$City\$>";
     $shuzu1[10]="<=\$SmallId\$>";
     $shuzu1[11]="<=\$CityId\$>";
     $shuzu1[12]="width=\"100%\"";
     $MyMake->OutUrl="News_show_1.shtml";
     $shuzu2[0]="數(shù)組0";
     $shuzu2[1]="數(shù)組1";
     $shuzu2[2]="數(shù)組2";
     $shuzu2[3]="數(shù)組3";
     $shuzu2[4]="數(shù)組4";        
     $shuzu2[5]="數(shù)組5";
     $shuzu2[6]="數(shù)組6";
     $shuzu2[7]="數(shù)組7";
     $shuzu2[8]="數(shù)組8";
     $shuzu2[9]="數(shù)組9";
     $shuzu2[10]="數(shù)組10";
     $shuzu2[11]="數(shù)組11";
     $shuzu2[12]="width=\"95%\"";
     $MyMake->Replacehtml($shuzu1,$shuzu2);
     $MyMake->SaveFile();
    //------------------靜態(tài)生成完成-----------

 

分享:Fedora下Zend Studio 6.1.2 如何配置
Zend Studio是我們PHPer的一個非常的得力的工具,有了他往往能夠讓我們的工作事倍功半,今天就來記錄一下日常使用中非常有用的配置,來幫助更多的PHPer提高工作的效率。 1、配置服務(wù)器。 如果我們希望能夠快速的進行調(diào)試,那么將Server和Zend整合在一起是必

來源:模板無憂//所屬分類:PHP教程/更新時間:2009-10-01
相關(guān)PHP教程