国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 岚皋县| 太康县| 稷山县| 革吉县| 双峰县| 内丘县| 乾安县| 丰宁| 屏东市| 红河县| 包头市| 怀集县| 沁水县| 崇左市| 那曲县| 垦利县| 浪卡子县| 榕江县| 金湖县| 高州市| 濮阳县| 甘洛县| 石屏县| 广东省| 北流市| 肥西县| 屯门区| 禄丰县| 兴山县| 馆陶县| 临湘市| 内丘县| 竹山县| 辉南县| 同江市| 丰城市| 贵溪市| 长兴县| 安远县| 张家口市| 乌拉特前旗|