Home > Backend Development > PHP Tutorial > Two general methods to generate static html pages using php

Two general methods to generate static html pages using php

WBOY
Release: 2016-07-29 09:15:26
Original
1043 people have browsed it

Because every time a user clicks on a dynamic link, a data query request will be sent to the server

For a website with potentially millions of visits, this is undoubtedly a huge burden on the server

So convert the dynamic data Turning it into a static HTML page has become the first choice to save manpower and material resources

Because I had no corresponding experience before, I thought this technology was very mysterious at first

But after reading some examples, I found that it is not that complicated (but the information on the Internet is not Particularly detailed)

After a morning and afternoon experiment, I finally completed the task. Here are some thoughts and a simple example

I hope you guys won’t laugh at me

Generally speaking, use php to convert and output html pages. There are two ways to quote Da Xia’s article as follows:

The first way: use templates. There are currently many PHP templates, including the powerful smarty and the simple and easy-to-use smarttemplate. Each of their templates has a function to get the output content. The way we generate static pages is to use this function. The advantage of using this method is that the code is clearer and more readable.

Here I use smarty as an example to illustrate how to generate a static page:

  1. require("smarty/Smarty.class.php");
  2. $t = new Smarty;
  3. $t ->assign("title","Hello World!");
  4. $content = $t->fetch("templates/index.htm");
  5. //The fetch() here is to get the output content function, now in the $content variable is the content to be displayed
  6. $fp = fopen("archives/2005/05/19/0001.html", "w");
  7. fwrite($fp, $content );
  8. fclose($fp);
  9. ?>

Second method: Use the ob series of functions. The functions used here are mainly ob_start(), ob_end_flush(), ob_get_content(), where ob_start() means to open the browser buffer. After opening the buffer, all non-file header information from the PHP program will not be sent. Instead, it is stored in the internal buffer until you use ob_end_flush(). The most important function here is ob_get_contents(). The function of this function is to obtain the contents of the buffer, which is equivalent to the fetch() above. The reason the same.

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

The second type I chose The method is to use the ob series of functions

I was a little unclear when I first read this. Later I learned that ob is output buffering, which means output caching

When you are ready to output, all the data is saved in ob. After the server parses php, all the html codes to be output to the client are stored in ob. If we want to output an html static page, we only need to take out the cache and write it into an html page

So the principle is actually very simple

Here Several functions are used. Since I am new to PHP and I don’t understand many functions, I will explain them here. I hope it can help everyone

ob_start(): Start “capturing” the cache, which means opening the browser’s cache from here

ob_end_flush( ): Turn off the browser cache

ob_get_content(): Read the cached content

fopen("File path", "Open mode") Open the file There are several opening modes for this function. The following are the main modes:

"r" Open in read-only mode and point the file pointer to the file header.

"r+" Open in reading and writing mode and point the file pointer to the file header.

"w" opens in writing mode, points the file pointer to the file header and truncates the file size to zero. If the file does not exist, try to create it.

"w+" Open in reading and writing mode, point the file pointer to the file header and truncate the file size to zero. If the file does not exist, try to create it.

fwrite("File name", "Write content") Write the file

fclose() Close the file

Since there are many html files I want to convert, there may be hundreds, so the path to fopen cannot be statically specified here. You can set a path variable in which you can save the ID and other information sent by the user to facilitate naming of HTML files. Here is a simple example of how I read xml data with PHP last time

  1. ob_start();//Open browser cache
  2. //The following is to read xml data
  3. $parser = xml_parser_create(); //Create a parser editor
  4. xml_set_element_handler($parser, "startElement", "endElement");//The corresponding functions when setting up tag triggers are startElement and endElenment respectively
  5. xml_set_character_data_handler($parser, "characterData");//When setting up data reading The corresponding function
  6. $xml_file="1.xml";//Specify the xml file to be read, which can be url
  7. $filehandler = fopen($xml_file, "r");//Open the file
  8. while ($data = fread($filehandler, 4096))
  9. {
  10. xml_parse($parser, $data, feof($filehandler));
  11. }//Take out 4096 bytes for processing each time
  12. fclose($filehandler);
  13. xml_parser_free($parser);//Close and release the parser parser
  14. $name=false;
  15. $position=false;
  16. function startElement($parser_instance , $element_name, $attrs) //Function of start tag event
  17. {
  18. global $name,$position;
  19. if($element_name=="NAME")
  20. {
  21. $name=true;
  22. $position=false;
  23. echo "Name:";
  24. }
  25. if($element_name=="POSITION")
  26. {$name=false;
  27. $position=true;
  28. echo "Position: ";
  29. }
  30. }
  31. function characterData($parser_instance, $xml_data) //Function when reading data
  32. {
  33. global $name,$position;
  34. if($position)
  35. echo $xml_data."
    ";
  36. if($name)
  37. echo $xml_data."
    ";
  38. }
  39. function endElement($parser_instance, $element_name ) //Function to end tag event
  40. {
  41. global $name,$position;
  42. $name=false;
  43. $position=false;
  44. }
  45. //Xml data reading is completed
  46. $htmlname=$id.".html";//$id can be defined by yourself. This represents the id passed by the user
  47. $htmlpath="archives/".$htmlname; //Set the path variable
  48. $content = ob_get_contents( );//Get all the content output by the php page
  49. $fp = fopen($htmlpath, "w");
  50. fwrite($fp, $content);
  51. fclose($fp);
  52. ?>

Reprinted: http://www.cnblogs.com/awinlei/archive/2013/03/04/2942962.html

The above introduces two common methods for generating static html pages using PHP, including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template