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