Home  >  Article  >  Backend Development  >  Three methods and code details for generating static pages in PHP

Three methods and code details for generating static pages in PHP

WBOY
WBOYOriginal
2016-07-25 09:05:48966browse
  1. ob_start();
  2. @readfile("http://bbs.it-home.org/");
  3. $text = ob_get_flush();
  4. $myfile = fopen("myfile .html","w");
  5. $text =
  6. str_replace ("{counent}",$string,$text);
  7. fwrite($myfile,$text);
  8. ob_clean();
  9. ?>
Copy the code

Because even if you want to generate a static page, the dynamic reading part must be retained. After inserting the data into the database, pass the url to the readfile function, then read it into the cache, and fwrite to generate the static page. This This is a practice that Tutu appreciates most. The fewest lines of code and the highest efficiency. http://bbs.it-home.org/ is a naked page, that is, pure content, without header, tail, or menu. In this way, you can customize your own template myfile.html more freely. If you only require the generation of static pages, this basically meets the needs.

2. Normally generate static html page fread the incoming page and then replace it with str_replace First, create the final content page:

  1. $title = "http://siyizhu.com test template";
  2. $file = "TwoMax Inter test templet,
    author: [email=Matrix@Two_Max]Matrix @Two_Max[/email]";
  3. $fp = fopen ("temp.html","r");
  4. $content = fread($fp,filesize ("temp.html"));
  5. $content = str_replace( "{file}",$file,$content);
  6. $content = str_replace("{title}",$title,$content);
  7. $filename = "test/test.html";
  8. $handle = fopen ( $filename,"w"); //Open the file pointer and create the file
  9. /* Check whether the file is created and writable*/
  10. if (!is_writable ($filename))
  11. {
  12. die ("File:".$ filename."Not writable, please check its properties and try again!");
  13. }
  14. if (!fwrite ($handle,$content))
  15. { //Write information to the file
  16. die ("Generate file".$ filename."Failed!");
  17. }
  18. fclose ($handle); //Close the pointer
  19. die ("Create file".$filename."Success!");
  20. ?>
Copy code

This step is just a simple variable replacement. If you want to generate a static list page, the principle is the same. Use a program to generate the article list, treat it as a large variable, and replace the variables in the template. This is how the page turning page of the list is. Of course, if the information is updated, the list page will also need to be regenerated.

  1. $title = "http://";
  2. $file = "TwoMax Inter test templet,
    author:[email=Matrix@Two_Max]Matrix@Two_Max[/email ]";
  3. $fp = fopen ("temp.html","r");
  4. $content = fread ($fp,filesize ("temp.html"));
  5. $content = str_replace ("{file}" ,$file,$content);
  6. $content = str_replace ("{title}",$title,$content);
  7. //Start generating list
  8. $list = '';
  9. $sql = "select id,title, filename from article";
  10. $query = mysql_query ($sql);
  11. while($result = mysql_fetch_array ($query))
  12. {
  13. $list .= ''.$result['title'].'
    ';
  14. }
  15. $content .= str_replace("{articletable}",$list,$content );//End of generating list
  16. // echo $content;
  17. $filename = "test/test.html";
  18. $handle = fopen ($filename,"w");
  19. //Open the file pointer and create the file
  20. /* Check whether the file is created and writable*/
  21. if(!is_writable ($filename))
  22. {
  23. die ("File: ".$filename." is not writable, please check its properties and try again!");
  24. }
  25. if(!fwrite($handle,$content))
  26. { //Write information to the file
  27. die ("Generate file".$filename."Failed!");
  28. }
  29. fclose($handle); //Close the pointer
  30. die ("Create file".$filename."Success!");
  31. ?>
Copy code

About turning pages: If we specify paging, there will be 20 articles per page. There are 45 articles in a certain sub-channel list according to the database query. First, we obtain the following parameters through query: 1, the total number of pages; 2, the number of articles per page. The second step, for ($i = 0; $i

  1. $fp = fopen ("temp.html","r");
  2. $content = fread ($fp,filesize ("temp.html"));
  3. $onepage = '20';
  4. $sql = "select id from article where channel='$channelid'";
  5. $query = mysql_query ($sql);
  6. $num = mysql_num_rows ($query);
  7. $allpages = ceil ($num / $onepage);
  8. for ($i = 0;$i<$allpages; $i++)
  9. {
  10. if ($i == 0)
  11. {
  12. $ indexpath = "index.html";
  13. }
  14. else
  15. {
  16. $indexpath = "index_".$i."html";
  17. }
  18. $start = $i * $onepage;
  19. $list = '';
  20. $ sql_for_page = "select name,filename,title from article where channel='$channelid' limit $start,$onepage";
  21. $query_for_page = mysql_query ($sql_for_page);
  22. while ($result = $query_for_page)
  23. {
  24. $list .= ''.$title.'
    ';
  25. }
  26. $content = str_replace ("{articletable}",$list,$content);
  27. if (is_file ($indexpath))
  28. {
  29. @unlink ($indexpath); //If the file already exists, delete it
  30. }
  31. $handle = fopen ( $indexpath,"w"); //Open the file pointer and create the file
  32. /*Check whether the file is created and writable*/
  33. if (!is_writable ($indexpath))
  34. {
  35. echo "File: ".$indexpath ."Not writable, please check its properties and try again!"; //Change to echo
  36. }
  37. if (!fwrite ($handle,$content))
  38. {//Write information to the file
  39. echo "Generate file" .$indexpath."Failed! "; //Modify to echo
  40. }
  41. fclose ($handle); //Close pointer
  42. }
  43. fclose ($fp);
  44. die ("Generating the paging file is completed. If the generation is incomplete, please check the file permission system and try again. Generate! ");
  45. ?>
Copy the code

3. Smarty template generates static pages smarty has its own fetch function, which is similar to fread() and can be used to generate static pages. Regarding smarty technology, you can read a few articles here: 1) Basic settings of smarty 2) Applications related to smarty caching 3) How to generate static pages using smarty 4) Detailed introduction to php template engine Smarty

  1. include("Smarty.class.php");
  2. $smarty = new Smarty;
  3. $smarty->caching = true;
  4. / / only do db calls if cache doesn't exist
  5. if(!$smarty->is_cached("index.tpl"))
  6. {// dummy up some data
  7. $address = "245 N 50th";
  8. $db_data = array("City" => "Lincoln", "State" => "Nebraska", "Zip" => "68502");
  9. $smarty->assign("Name","Fred") ;
  10. $smarty->assign("Address",$address);
  11. $smarty->assign($db_data);
  12. }// capture the output
  13. $output = $smarty->fetch("index. tpl");
  14. //This place is the key//do something with $output here
  15. echo $output; //hoho Have you seen the result of the output? Then what? After fwrite, we will get the result we want.
  16. $fp = fopen("archives/2013/05/19/0001.html", "w");
  17. fwrite($fp, $content);
  18. fclose($fp);
  19. ?>
Copy Code
Copy code The code is as follows:

  1. ob_start();
  2. echo "Hello World!";
  3. $content = ob_get_contents();//Get all the contents output by the php page
  4. $fp = fopen("archives/ 2013/05/19/0001.html", "w");
  5. fwrite($fp, $content);
  6. fclose($fp);
  7. ?>
Copy code

P.S.: Blog or forum programs that can generate static pages will "semi-automatically" generate HTML by manually clicking the "Generate HTML Page" button in the background.

Articles you may be interested in: Example of php generating static page function (php2html) How to generate static pages in php (three functions) Reference on how to generate html static pages with php A class written in php to generate static pages Code to generate html static pages from all content in the database How to automatically generate static pages on a virtual host at regular intervals Detailed tutorial on generating static pages with php Solution to the problem that pseudo-static pages cannot be accessed in apache Code written by php about spider crawling records of static pages How to generate static pages using smarty How to generate static pages with PHP Solution to the problem that apache cannot access pseudo-static pages



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