php靜態化類

來源:互聯網
上載者:User
一、靜態化類
複製以下代碼,存為檔案static.class.php

 1 Templet      = "";18         $this->DataSource   = array();    19         $this->fileName     = "";20         $this->mod          = "wb";21         $this->handle       = false;22     }23     //綁定資料來源,參數為一數組24     function BindData($arr)25     {26         $this->DataSource = $arr;27     }28     //設定路徑及檔案名稱29     function SetFileName($fileName)30     {31         return $this->fileName = $fileName;32     }33     function Open()34     {35         if ($this->handle = fopen($this->fileName, $this->mod))36             return $this->handle;37         else38             return false;39     }40     function Close()41     {42         return fclose($this->handle);43     }44     function Write($content)45     {46         return fwrite($this->handle, $content);47     }48     //建立目錄,支援絕對路徑與相對路徑,相對路徑相對於當前指令碼。linux下注意要有許可權49     function MkDir($pathname)50     {51         if($pathname){52             str_replace("\\", "/", $pathname);53             if(!file_exists($pathname)){54                 if(!@mkdir($pathname,0777)){55                     echo "建立檔案夾失敗";56                     exit;57                 }58             }59         }else{60             return false;61         }62     }63     // 產生靜態檔案64     function Create()65     {66         $tmp = $this->Templet;67         foreach ($this->DataSource as $key => $value) {68             $tmp = str_replace( $key , $value, $tmp);69         }70         $this->MkDir(dirname($this->fileName));71         $this->Open();72         $this->Write($tmp);73         $this->Close();74     }75 }76 ?>

二、使用方法
複製以下代碼,存為一個php檔案,如test.php

 1 "a","3"=>"c");//標籤替換規則12     CreateShtml($buffer,$filename,$filepath,$arr);13 }14 function CreateShtml($buffer,$filename,$filepath,$arr=array()) {15     $shtml = new Shtml();16     $shtml->SetFileName($filepath.$filename);17     $shtml->Templet = $buffer;18     $shtml->BindData($arr);19     $shtml->Create();20 }21 ?>
  • 聯繫我們

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