Home  >  Article  >  Backend Development  >  The principle of using PHP to generate static HTML documents_PHP tutorial

The principle of using PHP to generate static HTML documents_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:15:09839browse

Give the code:

Copy the code The code is as follows:

//Introduce the database configuration file
include( dirname(dirname(__FILE__))."includeconfig.php" );

/**
*
* Generate a single HTML file from the articles in the database.
* @param Date $Date
* @param Time $Time
* @param String $Content
* @ param String $Title
*/
function GenerateHTML($Date,$Time,$Content,$Title ,$Name){

//Decompose date and time variables into arrays
$GetDateRow = explode("-", $Date);
$GetTimeRow = explode(":",$ Time);

//Get the name of the file. For example: 20121028210632.html
$FileName = $GetDateRow[0].$GetDateRow[1].$GetDateRow[2].$GetTimeRow[0].$GetTimeRow[1].$GetTimeRow[2].".html ";

//Open and read the template content
$FP = fopen("tmp.html","r");
$Str = fread($FP,filesize("tmp .html"));

//Get the replaced template content
$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);

//Close the file to reduce the pressure on the server.
fclose($FP);

//Write the content to the HTML file
$Handle = fopen($FileName,"w");
fwrite($Handle,$Str) ;
fclose($Handle);

//Quick test
//echo "ok, done!";

}

//Database Operations
$querysql = "select * from article";
$queryset = mysql_query($querysql);

//Loop to generate HTML files.
while( $row = mysql_fetch_array($queryset) ){
GenerateHTML($row['date'],$row['time'],$row['content'],$row['title' ],$row['name']);
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326180.htmlTechArticle gives the code: Copy the code as follows: ?php //Introduce the database configuration file include( dirname(dirname(__FILE__ ))."includeconfig.php" ); /** * * Generate a single article from the database...
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