- 第一個方法是先建一個模板,在模板中有一些特殊的標記“{-標題-} {-背景-}”。把你的數據從TXT文本或數據庫中讀出,接著替換掉相應的標記,再寫入用當前時間命名的HTML文件中。簡單明了!
- ChinaZhuhai(請修改我的注冊信息)
先做一個靜態頁tpl.htm:
以下是它的代碼:1<html>
2<head><title>{-標題-}</title></head>
3<body bgcolor={-背景-}></body>
4</html>
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);
18if ($fputs)
19{
20echo '完成';
21fclose($fp);
22}
23?>
第二個方法原理是一樣的——替換相應標記。但它是從緩存中讀取的數據,再填充到HTML文件中。速度上更快些,而且作者把封裝成一個類!(編者按)
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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69
