Three common methods for generating static pages on PHP dynamic pages

Source: Internet
Author: User
This article mainly introduces three common methods for generating static pages in PHP dynamic pages. three different methods can be used to achieve this function from different perspectives, which provides great reference value, if you need a friend, you can refer to the page for generating static pages. it is very simple to define the template and template title, and then use str_replace to replace it. this is the most common method, the other method is to use the ob_get_contents output to obtain and generate html. The other method that is not recommended is to use file_get_contents to directly access remote files and save the files. The performance is very poor.

The specific method is 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 code is as follows:

$ 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

Method 1: ob_get_contents ()

This is a convenient and frequently used method. the implementation principle is: first open the cache, then create the corresponding static page file, write the cached content, and clear the cache.
Example:

The code is as follows:

Ob_strart (); // open the buffer
$ Fn = date ('ymdhis '). rand (). 'html'; // Generate a file name
Require ("supply. php"); // load the file to generate the static page, because there is ob_clean () in the background, it will not be displayed
$ Fs = fopen ($ fn, 'w'); // open a static page File
Fwrite ($ fs, ob_get_contents (); // Generate a static file
Ob_clean (); // clear the cache

Method 2: file_get_contents ();

The code is as follows:

$ Fn = date ('ymdhis '). rand (). 'html ';
$ Url = 'http: // '. $ _ SERVER ['http _ host']. "/"; // Note
$ Content = file_get_contents ($ url );
$ Fs = fopen ($ fn, 'w ');
Fwrite ($ fs, $ content );


The following explains how to use a reference file such as require ('header. php '); then header. the content in php cannot be displayed.

Method 3: str_replace ()

The code is as follows:

$ Filemodel = "supply. php"; string 5 $ file = fopen ($ filemodel, "w + ");
$ Temp = fread ($ file, filesize ($ filemodel ));
$ Temp = str_replace ("[title]", $ title, $ temp );
$ Temp = str_replace ("[postTime]", $ postTime, $ temp );
$ Temp = str_replace ("[content]", $ content, $ temp );


This method applies to simple pages. if supply. php uses a reference file such as require ('header. php'), the content in header. php cannot be displayed.
In practical applications, you can write a class that generates static pages,

The code is as follows:

/* ++
|
| Usage
| $ Shtml = new Shtml ($ Url, $ FileBag, $ FolderName, $ fileid)
| $ Url: URL of the page
| $ FileBag: the folder marked 1 as the specified folder
| 2: default folder (time (Year Month Day ))
| $ FolderRoot html file storage path
| $ FolderName: specifies the folder name $ When FileBag is set to 2, it can be left blank ("");
| $ Fileid static page name (the default suffix is. html)
|
|
|
/* ++ */
Class Shtml
{
Var $ message1 = "Error 1: You write class Shtml is Wrong! The second parameter is 1 or 2 in this class !. ";
Var $ message2 = "Error 2: The file write Error .";
Function _ construct ($ Url, $ FileBag, $ FolderRoot, $ FolderName, $ fileid)
{
$ This-> Url = $ Url;
$ This-> FileBag = $ FileBag;
$ This-> FileRoot = $ FolderRoot;
$ This-> FileName = $ FolderName;
$ This-> fileid = $ fileid;
Shtml: useFolder ();
}
*******************/
Public function loadcontent ($ Folder)
{
Ob_start ();
Require_once $ this-> Url;
Shtml: writehtml ($ Folder, ob_get_contents ());
Ob_clean ();
}
*****************/
Public function useFolder ()
{
If ($ this-> FileBag = 1)
{
$ Folder = $ this-> FileName;
}
Else if ($ this-> FileBag = 2)
{
$ Folder = date ('ymmd', time ());
}
Else
{
Exit ($ this-> message1 );
}
If (! Is_dir ($ this-> FileRoot. $ Folder) {mkdir ($ this-> FileRoot. $ Folder, 0700 );}
Shtml: loadcontent ($ Folder );
}
*****************/
Public function writehtml ($ Folder, $ cache_value)
{
$ File = fopen ($ this-> FileRoot. $ Folder. '/'. $ this-> fileid.'.html ', 'W + ');
Fwrite ($ file, $ cache_value );
Fclose ($ file );
}
}
$ Fileid = 2;
$ Shtml = new Shtml ("http://www.bitsCN.com", 1, "", "cc", $ fileid );


In summary, this html program code is not generated by page. if there are many articles, it only generates one article. if we want to improve the code, we will not introduce it here. If you are interested, you can test and improve it. I believe there will be great gains!

I hope this article will help you with PHP programming.

Related Article

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.