Home > php教程 > php手册 > PHP generates static HTML document implementation code,

PHP generates static HTML document implementation code,

WBOY
Release: 2016-07-06 14:24:53
Original
953 people have browsed it

PHP generates static HTML document implementation code.

Use PHP to generate a single HTML document from the article data in the database. First of all, it is beneficial to search engine inclusion. Secondly, it is safer to avoid exposing fields in the database to the address bar.
Give code:

<&#63;php
//引入数据库配置文件
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']);
}

Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone’s study. I also hope that everyone will support Bangke Home.

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template