搜尋
首頁php教程php手册php生成html静态页面的二种方法

在我之前所见的文章中要不是用代码堆砌空间就是用高手与高手交流用的语言让新人望而生却,因此本文尽量把整体思路说得详尽点.

两种方法简单说明如下:

一,利用PHP的输出控制函数(Output Control)得到静态页面字符串,再写入到新的文件中.

使用说明:

1、实例化,代码如下:

$cache = new Cache();

2、设置缓存时间和缓存目录

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

第一个参数是缓存秒数,第二个参数是缓存路径,根据需要配置,默认情况下,缓存时间是 3600 秒,缓存目录是 cache/.

3、读取缓存,代码如下:

<?php
$value = $cache->get(&#39;data_key&#39;);
4、写入缓存 $value = $cache->put(&#39;data_key&#39;, &#39;data_value&#39;);
完整实例: $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, &#39;w&#39;);
        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&#39;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
                
            } //开源代码phprm.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页面->将替换的内容写入新文件中->关闭新文件->生成成功;代码如下:

<?php
$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.&#39;&#39;.$contents.&#39;&#39;;
        $path = $key . &#39;.html&#39;;
        $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 "生成";
    }
?>


教程地址:

欢迎转载!但请带上文章地址^^

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能