把php產生靜態(html)頁面程式碼_PHP教程

來源:互聯網
上載者:User
產生靜態頁面一般是把動態網頁面產生html頁面,這樣可以減少伺服器負載也是現在各大網站常用的最佳化方法,下面我來分享一個把php產生靜態(html)頁面類。
代碼如下 複製代碼

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,

2 => $navigation,

3 => $happy_origin);

$htm = new Restore_email($template, $daytype);

echo $htm->pint();

?>


http://www.bkjia.com/PHPjc/444648.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444648.htmlTechArticle產生靜態頁面一般是把動態網頁面產生html頁面,這樣可以減少伺服器負載也是現在各大網站常用的最佳化方法,下面我來分享一個把php產生靜態...

  • 聯繫我們

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