在我之前所見的文章中要不是用代碼堆砌空間就是用高手與高手交流用的語言讓新人望而生卻。因此本文盡量把整體思路說得詳盡點。
兩種方法簡單說明如下:
一, 利用PHP的輸出控制函數(Output Control)得到靜態頁面字串,再寫入到新的檔案中。
使用說明:
1、執行個體化
| 代碼如下 |
複製代碼 |
$cache = new Cache();2、設定緩衝時間和緩衝目錄 $cache = new Cache(60, '/any_other_path/'); |
第一個參數是緩衝秒數,第二個參數是緩衝路徑,根據需要配置。
預設情況下,緩衝時間是 3600 秒,緩衝目錄是 cache/
3、讀取緩衝
| 代碼如下 |
複製代碼 |
$value = $cache->get('data_key');4、寫入緩衝 $value = $cache->put('data_key', 'data_value');完整執行個體: $cache = new Cache(); //從緩衝從讀取索引值 $key 的資料 $values = $cache->get($key); //如果沒有快取資料 if ($values == false) { //insert code here... //寫入索引值 $key 的資料 $cache->put($key, $values); } else { //insert code here... } Cache.class.php class Cache { private $cache_path;//path for the cache private $cache_expire;//seconds that the cache expires //cache constructor, optional expiring time and cache path public function Cache($exp_time=3600,$path="cache/"){ $this->cache_expire=$exp_time; $this->cache_path=$path; } //returns the filename for the cache private function fileName($key){ return $this->cache_path.md5($key); } //creates new cache files with the given data, $key== name of the cache, data the info/values to store public function put($key, $data){ $values = serialize($data); $filename = $this->fileName($key); $file = fopen($filename, 'w'); if ($file){//able to create the file fwrite($file, $values); fclose($file); } else return false; } //returns cache for the given key public function get($key){ $filename = $this->fileName($key); if (!file_exists($filename) || !is_readable($filename)){//can't read the cache return false; } if ( time() < (filemtime($filename) + $this->cache_expire) ) {//cache for the key not expired $file = fopen($filename, "r");// read data file if ($file){//able to open the file $data = fread($file, filesize($filename)); fclose($file); return unserialize($data);//return the values } else return false; } else return false;//was expired you need to create new } } ?> |
二, 利用模板產生
什麼是模板?如果大家使用過Dreamwerver中的“另存新檔模板”就應該知道模板是用來統一風格的東西。它只讓你修改頁面的某一部分,當然這“某一部分”是由你來確定的。本文在這說的模板也就是這個意思。(此外,PHP模板技術還包括phplib、smarty等等,這不是本文所說內容了)
把模板的概念結合本文再說得具體一點就是:美工先做好一個頁面,然後我們把這個頁面當作模板(要注意的是這個模板就沒必要使用EditRegion3這樣的代碼了,這種代碼是Dreamwerver為了方便自己設計而弄的標識),把這個模板中我們需要改變的地方用一個與HTML可以區分的字元代替,如“{title}”、“[title]”。在產生靜態頁面的時候只需要把資料和這些字串替換即可。這就是模板的含義了。
步驟:
1.建立一個php頁面和一個html頁面[模板頁];注:如果是從資料庫調用資料,則將資料以數組的形式儲存,然後迴圈產生;
2.在php頁面,開啟html頁面->讀取html頁面的內容->替換參數->建立(開啟)一個新的html頁面->將替換的內容寫入新檔案中->關閉新檔案->產生成功;
| 代碼如下 |
複製代碼 |
$open = fopen("template.htm","r"); //開啟模板檔案 $content = fread($open,filesize("template.htm")); //讀模數板檔案內容 //print_r($content); $content = str_replace("{title}","測試標題",$content);//替換 $content = str_replace("{contents}","測試內容",$content); $newtemp = fopen("1.htm","w");//產生,用寫入方式開啟一個不存在(新)的頁面 fwrite($newtemp,$content);//將剛剛替換的內容寫入新檔案中 fclose($newtemp); echo "產生"; |
php批量產生html測試:
| 代碼如下 |
複製代碼 |
//假設從資料庫中調的資料存放在二維數組$arr中 $arr = array(array("新聞標題一","新聞內容一"),array("新聞標題二","新聞內容二")); foreach($arr as $key=>$value){ $title = $value[0]; $contents = $value[1]; //echo $title.''.$contents.''; $path = $key.'.html'; $open = fopen("template.htm","r"); //開啟模板檔案 $handle = fread($open,filesize("template.htm")); //讀模數板檔案內容 $content = str_replace("{title}",$title,$handle);//替換 $content = str_replace("{contents}",$contents,$handle); $newtemp = fopen($path,"w");//用寫入方式開啟一個不存在(新)的頁面 fwrite($newtemp,$content);//將剛剛替換的內容寫入新檔案中 fclose($newtemp); echo "產生"; } |
http://www.bkjia.com/PHPjc/444662.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444662.htmlTechArticle在我之前所見的文章中要不是用代碼堆砌空間就是用高手與高手交流用的語言讓新人望而生卻。因此本文盡量把整體思路說得詳盡點。 兩種...