转载自CSDN.net论坛
第一个方法是先建一个模板,在模板中有一些特殊的标记“{-标题-} {-背景-}”。把你的数据从TXT文本或数据库中读出,接着替换掉相应的标记,再写入用当前时间命名的HTML文件中。简单明了!
第二个方法原理是一样的——替换相应标记。但它是从缓存中读取的数据,再填充到HTML文件中。速度上更快些,而且作者把封装成一个类!(编者按)
- ChinaZhuhai(请修改我的注册信息)
先做一个静态页tpl.htm:
以下是它的代码:
1
<html>
2
<head><title>{-标题-}</title></head>
3
<body bgcolor={-背景-}></body>
4
</html> 以下是程序do.php:
1
<?php
2
$files='xxx.htm';//这是生成后的页,名字一般用date(YmdHis)生成;
3
$tpl='tpl.htm';
4
$new_title='新标题';
5
$new_bg='#ffffff';
6
//先用fread调用$tpl
7
$fp=fopen($tpl,"r");
8
$tpl=fread($fp,filesize($tpl));
9
10
//替换;
11
$content=str_replace('{-标题-}',$new_title,$tpl);
12
$content=str_replace('{-背景-}',$new_bg,$tpl);
13
14
//存储新页;
15
16
$fp=fopen($files,"w");
17
$fputs=fputs($fp,$content);
18
if ($fputs)
19
{
20
echo '完成';
21
fclose($fp);
22
}
23
?> 如是linux主机,要将所在目录设为777属性
xuzuning(唠叨)
1
<?php
2
/**
3
* 作者: 徐祖宁 (唠叨)
4
*
5
* 类: outbuffer
6
* 功能: 封装部分输出控制函数,控制输出对象。
7
*
8
* 方法:
9
* run($proc) 运行php程序
10
* $proc php程序名
11
* display() 输出运行结果
12
* savetofile($filename) 保存运行结果到文件,一般可用于生成静态页面
13
* $filename 文件名
14
* loadfromfile($filename) 装入保存的文件
15
* $filename 文件名
16
*
17
* 示例:
18
* 1.直接输出
19
* require_once "outbuffer.php";
20
* $out = new outbuffer();
21
* $out->run("test.php");
22
* $out->display();
23
*
24
* 2.保存为文件
25
* require_once "outbuffer.php";
26
* require_once "outbuffer.php";
27
* $out = new outbuffer("test.php");
28
* $out->savetofile("temp.htm");
29
*
30
* 3.装入并输出文件
31
* require_once "outbuffer.php";
32
* $out = new outbuffer();
33
* $out->loadfromfile("temp.htm");
34
* $out->display();
35
*
36
*/
37
38
class outbuffer {
39
var $length;
40
var $buffer;
41
function outbuffer($proc="") {
42
$this->run($proc);
43
}
44
function run($proc="") {
45
ob_start();
46
include($proc);
47
$this->length = ob_get_length();
48
$this->buffer = ob_get_contents();
49
$this->buffer = eregi_replace("\r?\n","\r\n",$this->buffer);
50
ob_end_clean();
51
}
52
function display() {
53
echo $this->buffer;
54
}
55
function savetofile($filename="") {
56
if($filename == "") return;
57
$fp = fopen($filename,"w");
58
fwrite($fp,$this->buffer);
59
fclose($fp);
60
}
61
function loadfromfile($filename="") {
62
if($filename == "") return;
63
$fp = fopen($filename,"r");
64
$this->buffer = fread($fp,filesize($filename));
65
fclose($fp);
66
}
67
}
68
?>
69
posted on 2005-05-13 17:45
楚客 阅读(762)
评论(0) 编辑 收藏 所属分类:
转载