Home  >  Article  >  php教程  >  利用PHP生成静态HTML文档的原理

利用PHP生成静态HTML文档的原理

WBOY
WBOYOriginal
2016-06-13 11:56:58999browse

给出代码:

复制代码 代码如下:


//引入数据库配置文件
include( dirname(dirname(__FILE__))."\include\config.php" );

/**
*
* 将数据库中的文章生成单个HTML文件.
* @param Date $Date
* @param Time $Time
* @param String $Content
* @param String $Title
*/
function GenerateHTML($Date,$Time,$Content,$Title,$Name){

//将日期、时间变量分解成数组
$GetDateRow = explode("-", $Date);
$GetTimeRow = explode(":",$Time);

//得到文件的名字。比如:20121028210632.html
$FileName = $GetDateRow[0].$GetDateRow[1].$GetDateRow[2].$GetTimeRow[0].$GetTimeRow[1].$GetTimeRow[2].".html";

//打开并读取模板内容
$FP = fopen("tmp.html","r");
$Str = fread($FP,filesize("tmp.html"));

//得到替换后的模板内容
$Str = str_replace("{Title}",$Title, $Str);
$Str = str_replace("{Content}", $Content, $Str);
$Str = str_replace("{Name}", $Name, $Str);
$Str = str_replace("{Date}", $Date,$Str);
$Str = str_replace("{Time}", $Time, $Str);

//关闭文件,减少服务器的压力。
fclose($FP);

//将内容写入HTML文件
$Handle = fopen($FileName,"w");
fwrite($Handle,$Str);
fclose($Handle);

//小测一下
//echo "ok,done!";

}

//数据库的操作
$querysql = "select * from article";
$queryset = mysql_query($querysql);

//循环生成HTML文件。
while( $row = mysql_fetch_array($queryset) ){
GenerateHTML($row['date'],$row['time'],$row['content'],$row['title'],$row['name']);
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn