Two methods for generating static pages in PHP

Source: Internet
Author: User

PHP static page generation method

The two methods are described as follows:
1. Use the file function to get the template string of the static page, and then use the str_replace function to replace the content to be replaced and then write it into the new file.
2. Use the Output Control Function of PHP to obtain the static page string and then write it into the new file.

The following describes in detail.

I. Generate a template

What is a template? If you have used "Save as template" in dreamwerver, you should know that the template is used to unify the style. It only allows you to modify a part of the page. Of course, this "part" is determined by you. The template mentioned in this article also means this. (In addition, PHP template technology also includes phplib, smarty, and so on. This is not the content mentioned in this Article)

Combining the concept of a template with this article, let's talk about the specific point: the artist first makes a page and then treats this page as a template (note that this template is unnecessary <! -- Templatebegineditable name = "editregion3" --> editregion3 <! -- Templateendeditable -->CodeThis kind of code is the identifier that dreamwerver gets to facilitate its own design). We will replace this template with a character that can be distinguished from HTML, for example, "{Title}" and "[title]". When generating a static page, you only need to replace the data with these strings. This is the meaning of the template.

The following describes the specific implementation methods: make a template -- replace the content in the template with special characters in the template -- extract the content in the template and store it in a string (the content of this string is the HTML code and the above special characters) -- use the function to replace the special characters in the string with the content we need to display on the page -- write the replaced string to a new one. success in the HTM page!

Functions cannot be implemented without the help of PHP functions. The most important thing after knowing the overall idea is to find the relevant functions in the manual.

First, we need to extract the HTML code in the template and put it in a variable. The value of this variable is a string containing HTML. We can use the string fread (INT handle, int length) function to implement it. Take a closer look at the parameter "handle" in it as a file pointer, this means we have to open the template file first (PHP is quite troublesome, isn't it possible to get it in one step !!!). Well, we will continue to find the function that can open the file: Resource fopen (string filename, string mode [, bool use_include_path [, resource zcontext]), here we only need to pass in the first two parameters. The first parameter is the file name. Remember to make sure the path is correct. We recommend that you use "rb" for the second parameter ", "R" indicates that the file is opened in read-only mode and the file pointer is directed to the file header. "B" indicates that the binary mode is forced. In this manual, we recommend that you consider portability, we strongly recommend that you always use the 'B' flag when opening a file with fopen.
In this step, we can write the code as follows:

<? PHP
$ Filemodel = "template/it. php"; # template address
$ File = fopen ($ filemodel, "rb"); # Open the template and get the file pointer.
$ Temp = fread ($ file, filesize ($ filemodel); # obtain the HTML code of the template file.
?>

Note: If you just want to read the content of a file into a string and use file_get_contents (), the performance of the file is much better than that of the fread () code, but this function is supported in PhP5 ).

Step 2: Use the str_replace () function to replace the file characters, the replacement method is to replace the content retrieved from the database or the data obtained through the form with the special characters in the template. This step is very simple. You may not understand it at first, but you can see it at a glance at the Code:
<? PHP
$ Temp = str_replace ("[title]", $ title, $ temp );
?>
The above "" [title] "is a special character in the template file ([title], not" [title] "), "$ title" is the content we want to display on the page, and "$ Temp" is the HTML code of the template file.

If you still need to replace it, you can continue using the str_replace () function, for example:

<? PHP
$ Temp = str_replace ("[posttime]", $ posttime, $ temp );
$ Temp = str_replace ("[content]", $ content, $ temp );
?>

Step 3, that is, the first anti-operation: Write the previously processed template string to another file, and this file is the page that we can finally display externally. Now repeat the second steps similar to Step 1: open the file and write the file:

<? PHP
Fwrite (fopen ("$ FILENAME", "WB"), $ temp); # $ filename is the name of the static page file.
?>

Fwrite writes the string content to the file.

Similarly, you can use the file_put_contents function to write files, but remember to write files in PhP5.

In this way, we can use a template to generate a static page.

Ii. generate static pages using the Output Control Function

This is a little more advanced than the template generation, but once you understand its implementation ideas, it is very simple. The so-called meeting is not difficult, the difficult is probably the same thing.

This method is more practical than the template generation method. The template generation method is generally used for posting or modifyingArticleIn this way, the database can be obtained directly from the form, and the database does not need to be passed. If you need to retrieve data from the database and replace a large number of items, or the page you need is not just a simple replacement, such as the homepage of the site. In this case, it is necessary to consider using the output control function.

The output control function is used to set the buffer, and the output content in the buffer can be obtained. To obtain the output content, you only need to use three functions: ob_start (), ob_get_contents (), ob_end_clean ()

The method is as follows: Set the buffer start point (or set the output content start point) -- release content -- get content -- clear the buffer -- write the obtained content as a file.

Related functions are described as follows:
1. ob_start: Open the output buffer.
Function Format: void ob_start (void)
Note: When the buffer zone is activated, all requests from PHPProgramThe non-file header information is not sent, but is saved in the internal buffer zone.
To output the buffer content, you can use ob_end_flush () or flush () to output the buffer content.
2. ob_get_contents: returns the content of the internal buffer.
Usage: String ob_get_contents (void)
Note: This function returns the content in the Current Buffer. If the output buffer is not activated, false is returned.
3. ob_end_clean: Delete the content of the internal buffer and disable the internal buffer.
Usage: void ob_end_clean (void)
Note: This function will not output the content of the internal buffer but delete it!

Let's take a look at how we use the output buffer to generate static pages:

Let's use the function to implement it!
<? PHP

Function createstaticpage ($ sourcepage, $ objectpage) # source file, target file
{
# Two parameters are obtained. One is the source file address and the other is the static page address to be generated.
Global $ db; # used for database connection in $ sourcepage

Ob_start (); # Open the buffer, which is equivalent to a box used to store things.

Include $ sourcepage; # Release the page in the buffer zone. From this code, you can understand that it can be displayed when you view the $ sourcepage page separately! This is the key. If the ob_end_clean () function is not followed, you will see the content on the $ sourcepage page when executing the program.

$ Cons = ob_get_contents (); # obtain the content in the buffer. The content here is the HTML code! This is equivalent to transferring the items in the box to a person!

Ob_end_clean (); # Clear the contents of the buffer zone and transfer the contents in the box to others. Cleaning is the consequence of preventing others from seeing the contents of the box, you can see the content on the $ sourcepage page.

$ Fp = fopen ($ objectpage, "WB") or die ("Open File during static generation". $ objectpage. "error ");

Fwrite ($ FP, $ cons); # write HTML code to a static file!

Fclose ($ FP );

Return true;
}
?>

Summary:
1. Either method writes the content to the static page you want to generate.
2. use the first method when you can use the first method, because the second method requires you to adjust and display the content through the database (it can also be said that the displayed content is hidden, but in fact, the program in $ sourcepage is executed.
3. For other analyses based on the specific situation, you need to filter and perform other checks because the content submitted by the form may be used directly during template generation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.