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

Jul 29, 2016 am 09:15 AM
html nbsp parser position

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.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles