国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

把php生成靜態(tài)(html)頁面程序代碼

2024-09-04 11:44:09
字體:
供稿:網(wǎng)友

生成靜態(tài)頁面一般是把動態(tài)頁面生成html頁面,這樣可以減少服務(wù)器負載也是現(xiàn)在各大網(wǎng)站常用的優(yōu)化方法,下面我來分享一個把php生成靜態(tài)(html)頁面類.

  1. <?php 
  2.  
  3. class create_html { 
  4.  
  5. private $template
  6.  
  7. //模版 
  8.  
  9. private $file_name
  10.  
  11. //文件名 
  12.  
  13. private $array
  14.  
  15. //數(shù)據(jù)數(shù)組 
  16.  
  17. function __construct($file_name$template$array) { 
  18.  
  19. //構(gòu)造類 
  20.  
  21. $this->template = $this->read_file($template"r"); 
  22.  
  23. //讀取模板文件 
  24.  
  25. $this->file_name = $file_name
  26.  
  27. $this->array = $array
  28.  
  29. //數(shù)據(jù)數(shù)據(jù) 
  30.  
  31. $this->html(); 
  32.  
  33. //生成html 
  34.  
  35.  
  36. function html() { 
  37.  
  38. //生成html 
  39.  
  40. while (ereg ("{([0-9]+)}"$this->template, $regs)) { 
  41.  
  42. //循環(huán)模版中所能的{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. //把數(shù)據(jù)替換成html內(nèi)容 
  51.  
  52. $this->write_file($this->file_name, $this->template, "w+"); 
  53.  
  54. //生成HTML文件 
  55.  
  56.  
  57.  
  58. function read_file($file_url$method = "r") { 
  59.  
  60. //讀取文件 
  61.  
  62. $fp = @fopen($file_url$method); 
  63.  
  64. //打開文件 
  65.  
  66. $file_data = fread($fpfilesize($file_url)); 
  67.  
  68. //讀取文件信息 
  69.  
  70. return $file_data
  71.  
  72.  
  73. function write_file($file_url$data$method) { 
  74.  
  75. //寫入文件 
  76.  
  77. $fp = @fopen($file_url$method); 
  78.  
  79. //打開文件 
  80.  
  81. @flock($fp, LOCK_EX); 
  82.  
  83. //鎖定文件 
  84.  
  85. $file_data = fwrite($fp$data); 
  86.  
  87. //寫入文件 
  88.  
  89. fclose($fp); 
  90.  
  91. //關(guān)閉文件 
  92.  
  93. return $file_data
  94.  
  95.  
  96.  
  97. #例子———————- 
  98.  
  99. #讀取郵件回復模版———————————————————————————- 
  100.  
  101. $title = "標題"
  102.  
  103. $navigation = "瀏覽器"
  104.  
  105. $happy_origin = "作者"
  106.  
  107. $name = "test2.htm"
  108.  
  109. $template = "default_tmp.php"
  110.  
  111. //模版中用{1}{2}來替換 
  112.  
  113. $daytype = array(1 => $title
  114. //開源代碼Vevb.com 
  115. 2 => $navigation
  116.  
  117. 3 => $happy_origin); 
  118.  
  119. $htm = new Restore_email($template$daytype); 
  120.  
  121. echo $htm->pint(); 
  122.  
  123. ?> 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 正阳县| 丹阳市| 阳泉市| 双牌县| 定西市| 朔州市| 徐州市| 昂仁县| 开阳县| 九江县| 长垣县| 金门县| 全椒县| 吴桥县| 喀喇| 西乌珠穆沁旗| 阿瓦提县| 渭源县| 通江县| 钟祥市| 交城县| 兴业县| 赤城县| 宁都县| 襄樊市| 泸水县| 高碑店市| 宁城县| 岚皋县| 石景山区| 吴堡县| 阳春市| 赤壁市| 尉氏县| 青阳县| 衡东县| 凯里市| 冀州市| 镶黄旗| 揭东县| 乌苏市|