生成静态页面一般是把动态页面生成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();
-
-
-
- }
-
- function html() {
-
-
-
- while (ereg ("{([0-9]+)}", $this->template, $regs)) {
-
-
-
- $num = $regs[1];
-
-
-
- $this->template = ereg_replace("{".$num."}", $this->array[$num], $this->template);
-
-
-
- $this->write_file($this->file_name, $this->template, "w+");
-
-
-
- }
-
- }
-
- 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";
-
-
-
- $daytype = array(1 => $title,
-
- 2 => $navigation,
-
- 3 => $happy_origin);
-
- $htm = new Restore_email($template, $daytype);
-
- echo $htm->pint();
-
- ?>
|