生成靜態頁面一般是把動態頁面生成html頁面,這樣可以減少服務器負載也是現在各大網站常用的優化方法,下面我來分享一個把php生成靜態(html)頁面類.
- <?php
- class create_html {
- private $template;
- //模版
- private $file_name;
- //文件名
- private $array;
- //數據數組
- function __construct($file_name, $template, $array) {
- //構造類
- $this->template = $this->read_file($template, "r");
- //讀取模板文件
- $this->file_name = $file_name;
- $this->array = $array;
- //數據數據
- $this->html();
- //生成html
- }
- function html() {
- //生成html
- while (ereg ("{([0-9]+)}", $this->template, $regs)) {
- //循環模版中所能的{1}…..
- $num = $regs[1];
- //得到1、2、3序列
- $this->template = ereg_replace("{".$num."}", $this->array[$num], $this->template);
- //把數據替換成html內容
- $this->write_file($this->file_name, $this->template, "w+");
- //生成HTML文件
- }
- }
- function read_file($file_url, $method = "r") {
- //讀取文件
- $fp = @fopen($file_url, $method);
- //打開文件
- $file_data = fread($fp, filesize($file_url));
- //讀取文件信息
- return $file_data;
- }
- function write_file($file_url, $data, $method) {
- //寫入文件
- $fp = @fopen($file_url, $method);
- //打開文件
- @flock($fp, LOCK_EX);
- //鎖定文件
- $file_data = fwrite($fp, $data);
- //寫入文件
- fclose($fp);
- //關閉文件
- return $file_data;
- }
- }
- #例子———————-
- #讀取郵件回復模版———————————————————————————-
- $title = "標題";
- $navigation = "瀏覽器";
- $happy_origin = "作者";
- $name = "test2.htm";
- $template = "default_tmp.php";
- //模版中用{1}{2}來替換
- $daytype = array(1 => $title,
- //開源代碼Vevb.com
- 2 => $navigation,
- 3 => $happy_origin);
- $htm = new Restore_email($template, $daytype);
- echo $htm->pint();
- ?>
新聞熱點
疑難解答