在我之前所見的文章中要不是用代碼堆砌空間就是用高手與高手交流用的語言讓新人望而生卻,因此本文盡量把整體思路說得詳盡點.
兩種方法簡單說明如下:
一,利用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
- <?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
- }//開源代碼Vevb.com
- 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 "生成";
- }
新聞熱點
疑難解答