本款生成静态页面程序实现原理是做好自定的模板标签,然后由str_replace把标签替换成指定的内容,再由fopen生成指定 文件名的静态页面,这样就OK了,代码如下:
- header('content-type:text/html;charset=utf-8');
- if(!function_exists('file_get_contents')){
- function file_get_contents($file){
- $fp = fopen($file,'r');
- $content = fread($fp,filesize($file));
- fclose($fp);
- return $content;
- }
- }
- $tmp_file = 'template.html';
- $content = file_get_contents($tmp_file);
- $title = 'title';
- $text = 'text';
- $content = str_replace('<{title}>',$title,$content);
- $content = str_replace('<{text}>',$text,$content);
-
- makehtml('news.html',$content);
- echo '<a href="news.html" target="_blank">查看文件</a>';
- function makehtml($file,$content){
-
- $fp = fopen($file,'w');
- fwrite($fp,$content);
- fclose($fp);
- }
template.html,代码如下:
- <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8" />
- <title>makehtml</title>
- </head>
- <body>
- 这是模板变量title------<{title}>
- <br />
- 这是模板变量text------<{text}>
- </body>
- </html>
|