Three Methods of Generating Static Pages from PHP Dynamic Pages

  • 2021-08-03 09:26:35
  • OfStack

It is very simple to generate static pages, that is, to define templates and template titles, and then replace them with str_replace, which is the most commonly used method. Another one is to use ob_get_contents output to obtain and then generate html, and another one is not recommended to use file_get_contents to directly access remote files and save them, which has poor performance.

The specific method is simply 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 what needs to be replaced and write it to a new file.
2. Use the output control function of PHP (Output Control) to get the static page string and write it to a new file.

$filemodel="template/it.php";           // Template address 
$file=fopen($filemodel,"rb");           // Open the template and get the file pointer
$temp=fread($file,filesize($filemodel));    // Get the template file html Code

Method 1: ob_get_contents ()

This is a very convenient method, is also a very common method, the implementation principle is: first open the cache, and then create the corresponding static page file, write the contents of the cache, empty the cache.

ob_strart();// Open buffer 
$fn=date('ymdhis').rand(1000,9999).'html';// Generate file name
require("supply.php");// Load the file where you want to generate the static page, because there is a ob_clean() So it won't show up now
$fs=fopen($fn,'w');// Open a static page file
fwrite($fs,ob_get_contents());// Generate static files
ob_clean();// Empty the cache

Method 2: file_get_contents ();

$url= 'http://'.$_SERVER['HTTP_HOST']."/";// Attention

If you are using only the filename instead of URL, then if you are using a reference file such as require ('header. php'); Then the contents in header. php will not be displayed.

Method 3: str_replace ()

$filemodel="supply.php";  String 5$file=fopen($filemodel,"w+");

This method is suitable for very simple pages, if there are reference files such as require ('header. php') in supply. php; Then the contents in header. php will not be displayed
In a practical application, you can write a class that generates static pages.
| Usage
|   $shtml = new Shtml($Url,$FileBag,$FolderName,$fileid)
|   $Url:       Page URL Address
|   $FileBag:   Folder tag    1 For : Specify a folder  
|         2 For : Default folder ( Time ( Year, month, day ))
|        $FolderRoot html File storage path
|   $FolderName Specify the name of the folder $FileBag For 2 Hour Can be written as null ("");
|   $fileid      Static page name ( Suffix Default to .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 ();
/************* Get data *******************/
public function loadcontent ($Folder)
require_once $this->Url;
Shtml::writehtml ($Folder,ob_get_contents());
/********** Specify a folder *****************/
public function useFolder ()
else if($this->FileBag==2)
if(!is_dir($this->FileRoot.$Folder)){ mkdir($this->FileRoot.$Folder,0700);}
Shtml::loadcontent ($Folder);
/********** Generate static pages *****************/
public function writehtml ($Folder,$cache_value)
$file   = fopen($this->FileRoot.$Folder.'/'.$this->fileid.'.html','w+');
$shtml = new Shtml("",1,"","cc",$fileid);

To sum up, this generation of html program code does not generate paging, if there are many articles, it only generates 1 article, if we need to make major changes to improve, we will not introduce it here. Interested friends can test and improve 1, and I believe there will be no small harvest!

I hope this article is helpful to everyone's PHP programming.

Related articles: