php 產生靜態檔案執行個體類函數
//-----------------------------產生靜態類-------------------------------
class Makehtml{
public $MbUrl,$OutUrl,$AllHtml,$SouChar,$ObjChar; //變數
public $row; //遊標
public $Shuzusou,$Shuzuobj; //替換的字串數組
//-----------------------初始化-------------------------
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);
}
//------------------自動擷取要替換的字串-------------------
}
//-----------------------------自動按欄位替換完成------------------------
//-----------------------------批量替換數組--------------------------
function Replacehtml($Shuzusou,$Shuzuobj){ //批量替換數組
if(count($Shuzusou)!=count($Shuzuobj)){
exit("替換數組不匹配");
}
if($this->AllHtml==""){
exit("沒有要替換的內容");
}
for($i=0;$i<count($Shuzusou);$i++){
$this->AllHtml=str_replace($Shuzusou[$i],$Shuzuobj[$i],$this->AllHtml);
//print("<br>".$Shuzusou(i)."=".$Shuzuobj(i)."<br>")
}
}
//-----------------------------批量替換數組完成--------------------------
//-----------------------------讀取檔案---------------------------------
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);
}
//-----------------------------儲存檔案完成------------------------------
}
執行個體
$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]="數組0";
$shuzu2[1]="數組1";
$shuzu2[2]="數組2";
$shuzu2[3]="數組3";
$shuzu2[4]="數組4";
$shuzu2[5]="數組5";
$shuzu2[6]="數組6";
$shuzu2[7]="數組7";
$shuzu2[8]="數組8";
$shuzu2[9]="數組9";
$shuzu2[10]="數組10";
$shuzu2[11]="數組11";
$shuzu2[12]="width="95%"";
$MyMake->Replacehtml($shuzu1,$shuzu2);
$MyMake->SaveFile();