Home  >  Article  >  Backend Development  >  Analysis of the principle of generating static html files in php

Analysis of the principle of generating static html files in php

WBOY
WBOYOriginal
2016-07-25 09:03:39843browse
  1. //Introduce the database configuration file

  2. include( dirname(dirname(__FILE__))."includeconfig.php" );

  3. / **

  4. *
  5. * Generate a single HTML file from the articles in the database.
  6. * @param Date $Date
  7. * @param Time $Time
  8. * @param String $Content
  9. * @param String $Title
  10. */
  11. function GenerateHTML($Date,$Time,$Content,$Title,$Name){

  12. //Decompose date and time variables into arrays

  13. $GetDateRow = explode("-", $Date);
  14. $GetTimeRow = explode(":",$Time);

  15. //Get the name of the file. For example: 20121028210632.html

  16. $FileName = $GetDateRow[0].$GetDateRow[1].$GetDateRow[2].$GetTimeRow[0].$GetTimeRow[1].$GetTimeRow[2].".html";

  17. //Open and read the template content

  18. $FP = fopen("tmp.html","r");
  19. $Str = fread($FP,filesize("tmp.html "));

  20. //Get the replaced template content

  21. $Str = str_replace("{Title}",$Title, $Str);
  22. $Str = str_replace("{Content }", $Content, $Str);
  23. $Str = str_replace("{Name}", $Name, $Str);
  24. $Str = str_replace("{Date}", $Date,$Str);
  25. $ Str = str_replace("{Time}", $Time, $Str);

  26. //Close the file to reduce the pressure on the server.

  27. fclose($FP);

  28. //Write the content to the HTML file

  29. $Handle = fopen($FileName,"w");
  30. fwrite($Handle,$Str);
  31. fclose($Handle);

  32. //Test

  33. //echo "ok,done!";
  34. }

  35. //Database operation

  36. $querysql = "select * from article";
  37. $queryset = mysql_query($querysql);

  38. //Loop to generate HTML files.

  39. while( $row = mysql_fetch_array($queryset) ){
  40. GenerateHTML($row['date'],$row['time'],$row['content'],$row['title'],$row ['name']);
  41. }
  42. ?>

Copy code


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