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

首頁 > 開發 > PHP > 正文

php生成html靜態頁面的二種方法

2024-05-04 21:48:39
字體:
來源:轉載
供稿:網友

在我之前所見的文章中要不是用代碼堆砌空間就是用高手與高手交流用的語言讓新人望而生卻,因此本文盡量把整體思路說得詳盡點.

兩種方法簡單說明如下:

一,利用PHP的輸出控制函數(Output Control)得到靜態頁面字符串,再寫入到新的文件中.

使用說明:

1、實例化,代碼如下:

$cache = new Cache();

2、設置緩存時間和緩存目錄

$cache = new Cache(60,'/any_other_path/');

第一個參數是緩存秒數,第二個參數是緩存路徑,根據需要配置,默認情況下,緩存時間是 3600 秒,緩存目錄是 cache/.

3、讀取緩存,代碼如下:

  1. $value = $cache->get('data_key');4、寫入緩存 
  2.  
  3. $value = $cache->put('data_key''data_value');完整實例: 
  4.  
  5. $cache = new Cache(); 
  6.  
  7. //從緩存從讀取鍵值 $key 的數據 
  8. $values = $cache->get($key); 
  9.  
  10. //如果沒有緩存數據 
  11. if ($values == false) { 
  12.  //insert code here... 
  13.  //寫入鍵值 $key 的數據 
  14.  $cache->put($key$values); 
  15. else { 
  16.  //insert code here... 

Cache.class.php

  1. <?php 
  2. class Cache { 
  3.  private $cache_path;//path for the cache 
  4.  private $cache_expire;//seconds that the cache expires 
  5.  
  6.  //cache constructor, optional expiring time and cache path 
  7.  public function Cache($exp_time=3600,$path="cache/"){ 
  8.   $this->cache_expire=$exp_time
  9.   $this->cache_path=$path
  10.  } 
  11.  
  12.  //returns the filename for the cache 
  13.  private function fileName($key){ 
  14.   return $this->cache_path.md5($key); 
  15.  } 
  16.  
  17.  //creates new cache files with the given data, $key== name of the cache, data the info/values to store 
  18.  public function put($key$data){ 
  19.   $values = serialize($data); 
  20.   $filename = $this->fileName($key); 
  21.   $file = fopen($filename'w'); 
  22.      if ($file){//able to create the file 
  23.          fwrite($file$values); 
  24.          fclose($file); 
  25.      } 
  26.      else return false; 
  27.  } 
  28.  
  29.  //returns cache for the given key 
  30.  public function get($key){ 
  31.   $filename = $this->fileName($key); 
  32.   if (!file_exists($filename) || !is_readable($filename)){//can't read the cache 
  33.    return false; 
  34.   } 
  35.   if ( time() < (filemtime($filename) + $this->cache_expire) ) {//cache for the key not expired 
  36.    $file = fopen($filename"r");// read data file 
  37.          if ($file){//able to open the file 
  38.              $data = fread($filefilesize($filename)); 
  39.              fclose($file); 
  40.              return unserialize($data);//return the values 
  41.          }//開源代碼Vevb.com 
  42.          else return false; 
  43.   } 
  44.   else return false;//was expired you need to create new 
  45.   } 
  46. ?> 

二,利用模板生成

什么是模板?如果大家使用過Dreamwerver中的“另存為模板”就應該知道模板是用來統一風格的東西,它只讓你修改頁面的某一部分,當然這“某一部分”是由你來確定的,本文在這說的模板也就是這個意思,此外,PHP模板技術還包括phplib、smarty等等,這不是本文所說內容了.

把模板的概念結合本文再說得具體一點就是:美工先做好一個頁面,然后我們把這個頁面當作模板(要注意的是這個模板就沒必要使用EditRegion3這樣的代碼了,這種代碼是Dreamwerver為了方便自己設計而弄的標識),把這個模板中我們需要改變的地方用一個與HTML可以區分的字符代替,如“{title}”、“[title]”。在生成靜態頁面的時候只需要把數據和這些字符串替換即可。這就是模板的含義了.

步驟:

1.新建一個php頁面和一個html頁面[模板頁];注:如果是從數據庫調用數據,則將數據以數組的形式保存,然后循環生成;

2.在php頁面,打開html頁面->讀取html頁面的內容->替換參數->新建(打開)一個新的html頁面->將替換的內容寫入新文件中->關閉新文件->生成成功;代碼如下:

  1. $open = fopen("template.htm","r"); //打開模板文件 
  2. $content = fread($open,filesize("template.htm")); //讀取模板文件內容 
  3. //print_r($content); 
  4. $content = str_replace("{title}","測試標題",$content);//替換 
  5. $content = str_replace("{contents}","測試內容",$content); 
  6.  
  7. $newtemp = fopen("1.htm","w");//生成,用寫入方式打開一個不存在(新)的頁面 
  8. fwrite($newtemp,$content);//將剛剛替換的內容寫入新文件中 
  9. fclose($newtemp); 
  10. echo "生成"

php批量生成html測試,代碼如下:

  1. //假設從數據庫中調的數據存放在二維數組$arr中 
  2. $arr = array(array("新聞標題一","新聞內容一"),array("新聞標題二","新聞內容二"));  
  3.  
  4. foreach($arr as $key=>$value){ 
  5.  $title = $value[0]; 
  6.  $contents = $value[1]; 
  7.  //echo $title.''.$contents.''; 
  8.  $path = $key.'.html'
  9.  $open = fopen("template.htm","r"); //打開模板文件 
  10.  $handle = fread($open,filesize("template.htm")); //讀取模板文件內容 
  11.  
  12.  $content = str_replace("{title}",$title,$handle);//替換 
  13.  $content = str_replace("{contents}",$contents,$handle); 
  14.  
  15.  $newtemp = fopen($path,"w");//用寫入方式打開一個不存在(新)的頁面 
  16.  fwrite($newtemp,$content);//將剛剛替換的內容寫入新文件中 
  17.  fclose($newtemp); 
  18.  echo "生成"

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 晋州市| 抚松县| 兴海县| 屯留县| 沙湾县| 江达县| 镇雄县| 德阳市| 论坛| 贺州市| 乡宁县| 共和县| 时尚| 城市| 托克托县| 唐河县| 安康市| 开鲁县| 东辽县| 溧阳市| 奉贤区| 临海市| 东乌珠穆沁旗| 徐闻县| 铜陵市| 易门县| 韶山市| 阿勒泰市| 获嘉县| 静海县| 双鸭山市| 元阳县| 海安县| 长春市| 平湖市| 岳池县| 额尔古纳市| 怀远县| 民权县| 东方市| 西藏|