The page for generating static pages is very simple: Defining the template and template title, and then replacing it with str_replace, which is the most common method, the other is to use the ob_get_contents output to obtain and generate html, and the other is not very popular... the page for generating static pages is very simple: Defining the template and template title, and then replacing it with str_replace, which 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 the remote file and save it. The performance is very poor.
The 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 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 to first open the cache, then create the corresponding static page file, write the cached content, and clear the cache.
Sample code:
Ob_strart (); # Open the buffer $ fn = date ('ymdhis '). rand (1000,9999 ). 'HTML '; // Generate the file name require ("supply. php "); # load the file to generate the static page, because the background has ob_clen (), so it will not be displayed $ fs = fopen ($ fn, 'w '); # Open the static page file fwrite ($ fs, ob_get_contents (); # Generate the static file ob_clean (); # clear the cache
Method 2: file_get_contents ();The code is as follows:
$ Fn = date ('ymdhis '). rand (1000,9999 ). '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 practice, you can write a class that generates static pages. the code is as follows:
/* ++ | Author: Chen Yunduan | usage | $ shtml = new Shtml ($ Url, $ FileBag, $ FolderName, $ fileid) | $ Url: page URL | $ FileBag: the folder is marked with 1 as the specified folder | 2 as the default folder (time (Year Month Day )) | $ FolderRoot html file storage path | $ FolderName specifies the folder name $ When FileBag is 2, it can be left blank (""); | $ fileid static page name (default suffix. 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; Shtm L: 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.phprm.com ", 1," "," cc ", $ fileid );
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.