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 then write it into the new file.
The code is as follows: |
Copy code |
$ 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: |
Copy code |
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_clen () in the background, it will not be displayed $ Fs = fopen ($ fn, 'w'); # Open a static page File Fwrite ($ fs, ob_get_contents (); # generate static files Ob_clean (); # clear cache
|
Method 2: file_get_contents ();
The code is as follows: |
Copy code |
$ 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: |
Copy code |
$ 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: |
Copy code |
/* ++ | | Author: Chen Yunduan | 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.111cn.net", 1, "", "cc", $ fileid ); |
To sum up, 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 need to make major changes, I will not introduce it here.