php专区

 首页 > php专区 > PHP应用 > php类库 > 把php生成静态(html)页面程序代码 - php类库

把php生成静态(html)页面程序代码 - php类库

分享到:
【字体:大 中 小】
导读:
          生成静态页面一般是把动态页面生成html页面,这样可以减少服务器负载也是现在各大网站常用的优化方法,下面我来分享一个把php生成静态(html)页面类.?phpclasscreate_html{private$template;//模版...

把php生成静态(html)页面程序代码

生成静态页面一般是把动态页面生成html页面,这样可以减少服务器负载也是现在各大网站常用的优化方法,下面我来分享一个把php生成静态(html)页面类.

  1.  
  2. class create_html { 
  3.  
  4. private $template; 
  5.  
  6. //模版 
  7.  
  8. private $file_name; 
  9.  
  10. //文件名 
  11.  
  12. private $array; 
  13.  
  14. //数据数组 
  15.  
  16. function __construct($file_name, $template, $array) { 
  17.  
  18. //构造类 
  19.  
  20. $this->template = $this->read_file($template, "r"); 
  21.  
  22. //读取模板文件 
  23.  
  24. $this->file_name = $file_name; 
  25.  
  26. $this->array = $array; 
  27.  
  28. //数据数据 
  29.  
  30. $this->html(); 
  31.  
  32. //生成html 
  33.  
  34. } 
  35.  
  36. function html() { 
  37.  
  38. //生成html 
  39.  
  40. while (ereg ("{([0-9]+)}", $this->template, $regs)) { 
  41.  
  42. //循环模版中所能的{1}….. 
  43.  
  44. $num = $regs[1]; 
  45.  
  46. //得到1、2、3序列 
  47.  
  48. $this->template = ereg_replace("{".$num."}", $this->array[$num], $this->template); 
  49.  
  50. //把数据替换成html内容 
  51.  
  52. $this->write_file($this->file_name, $this->template, "w+"); 
  53.  
  54. //生成HTML文件 
  55.  
  56. } 
  57.  
  58. } 
  59.  
  60. function read_file($file_url, $method = "r") { 
  61.  
  62. //读取文件 
  63.  
  64. $fp = @fopen($file_url, $method); 
  65.  
  66. //打开文件 
  67.  
  68. $file_data = fread($fp, filesize($file_url)); 
  69.  
  70. //读取文件信息 
  71.  
  72. return $file_data; 
  73.  
  74. } 
  75.  
  76. function write_file($file_url, $data, $method) { 
  77.  
  78. //写入文件 
  79.  
  80. $fp = @fopen($file_url, $method); 
  81.  
  82. //打开文件 
  83.  
  84. @flock($fp, LOCK_EX); 
  85.  
  86. //锁定文件 
  87.  
  88. $file_data = fwrite($fp, $data); 
  89.  
  90. //写入文件 
  91.  
  92. fclose($fp); 
  93.  
  94. //关闭文件 
  95.  
  96. return $file_data; 
  97.  
  98. } 
  99.  
  100. } 
  101.  
  102. #例子———————- 
  103.  
  104. #读取邮件回复模版———————————————————————————- 
  105.  
  106. $title = "标题"; 
  107.  
  108. $navigation = "浏览器"; 
  109.  
  110. $happy_origin = "作者"; 
  111.  
  112. $name = "test2.htm"; 
  113.  
  114. $template = "default_tmp.php"; 
  115.  
  116. //模版中用{1}{2}来替换 
  117.  
  118. $daytype = array(1 => $title, 
  119. //开源代码phpfensi.com 
  120. 2 => $navigation, 
  121.  
  122. 3 => $happy_origin); 
  123.  
  124. $htm = new Restore_email($template, $daytype); 
  125.  
  126. echo $htm->pint(); 
  127.  
  128. ?> 
分享到:
php文件缓存类文件 - php类库
php文件缓存类文件 本人给大家推一个不错php文件缓存类文件,从各方面来看本缓存类很合理并且适用于大型网站使用,php文件缓存类文件代码如下:
PHP获取汉字的拼音字母实现程序 - php类...
PHP获取汉字的拼音字母实现程序 一个完整的php获取汉字拼音字母的实现程序,有需要的朋友可参考一下,php实例代码如下:  
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……