PHP generated static page method with the implementation of code detailed version

  • 2020-03-31 20:19:28
  • OfStack

Fread () and fwirte() are the main ones used in PHP. Once the static page is generated, the problem of modification is involved. Regular matching can be used to replace the changed parts of a template. However this kind of method is too troublesome, the method worth recommending is to cut down the original generated template directly, regenerate, ha ha, the real one hundred.
It is also important to note that this method of generating static pages is generally used for pages that change infrequently, such as the final page of information. For list pages, it is also desirable if the information is not updated very frequently. Many blogs or forum programs that can generate static pages are popular on the Internet now, which are "semi-automatic" to generate HTML by manually clicking the "generate HTML page" button in the background. For some very large information portals, it does not work. Because static pages are called "static" because they cannot be changed automatically. If the information list is updated 100 times a day, the static list page will be regenerated 100 times. If I had 10 of them, I'd be vomiting blood.
Now, without further ado, let's look at the actual demo:
First: is an ob function to achieve the code is relatively simple, relatively high efficiency.
 
<?php 
ob_start(); 
@readfile("http://tools.jb51.net/"); 
$text = ob_get_flush(); 
$myfile = fopen("myfile.html","w"); 
$text = 
str_replace ("{counent}",$string,$text); 
fwrite($myfile,$text); 
ob_clean(); 
?> 

Even if you want to generate a static page, the dynamic reading part should be reserved. After inserting the data into the database, pass the url to the readfile function, and then read it into the cache. Fwrite can generate a static page. Least lines of code, most efficient. http://tools.jb51.net/ is a bare page, that is, pure content, with no header, tail, or menu. In order to compare the freedom to customize their own template myfile.html. If the only requirement is to generate static pages, this is basically what you need.
Second: normally generates static HTML pages.
This is done in a step-by-step fashion, with fread coming in and str_replace
The first is to create the final content page:
The PHP code
 
<?php 
$title = "http://siyizhu.com The test template "; 
$file = "TwoMax Inter test templet,<br>author : [email=Matrix@Two_Max]Matrix@Two_Max[/email]"; 
$fp = fopen ("temp.html","r"); 
$content = fread($fp,filesize ("temp.html")); 
$content = str_replace("{file}",$file,$content); 
$content = str_replace("{title}",$title,$content); 
$filename = "test/test.html"; 
$handle = fopen ($filename,"w"); //Open the file pointer and create the file
 
if (!is_writable ($filename)) 
{ 
die (" File: ".$filename." Unable to write, please check its properties and try again! "); 
} 
if (!fwrite ($handle,$content)) 
{ //Writes the information to a file
die (" Generate the file ".$filename." Failure! "); 
} 
fclose ($handle); //Close the pointer
die (" Create a file ".$filename." Success! "); 
?> 

This is the easy part. It's just a simple substitution. If you want to generate a static list page, the principle is the same. Use the program to generate the list of articles, treat it as a large variable, and replace the variables in the template. Of course, the list page will be regenerated if there is any information updated.
The PHP code
 
<?php 
$title = "http://"; 
$file = "TwoMax Inter test templet,<br>author : [email=Matrix@Two_Max]Matrix@Two_Max[/email]"; 
$fp = fopen ("temp.html","r"); 
$content = fread ($fp,filesize ("temp.html")); 
$content = str_replace ("{file}",$file,$content); 
$content = str_replace ("{title}",$title,$content); 
//Generate list start
$list = ''; 
$sql = "select id,title,filename from article"; 
$query = mysql_query ($sql); 
while($result = mysql_fetch_array ($query)) 
{ 
$list .= '<a href='.$root.$result['filename'].' target=_blank>'.$result['title'].'</a><br>'; 
} 
$content .= str_replace("{articletable}",$list,$content);//End of generated list
// echo $content; 
$filename = "test/test.html"; 
$handle = fopen ($filename,"w"); 
//Open the file pointer and create the file
 
if(!is_writable ($filename)) 
{ 
die (" File: ".$filename." Unable to write, please check its properties and try again! "); 
} 
if(!fwrite($handle,$content)) 
{ //Writes the information to a file
die (" Generate the file ".$filename." Failure! "); 
} 
fclose($handle); //Close the pointer
die (" Create a file ".$filename." Success! "); 
?> 

About turning pages:
If we specify paging, 20 pages per page. The article in a subchannel list is 45 by the database query, then, first of all, we get the following parameters through the query: 1, the total number of pages; 2. Number of pages per page. Step 2, for ($I = 0; $I < Allpages; $i++), page element acquisition, analysis, article generation, all execute in this loop. The difference is that die (" create file ".$filename." successful!" ; This sentence is removed and displayed after the loop, because this statement will abort the execution of the program.
Ex. :
The PHP code
 
<?php 
$fp = fopen ("temp.html","r"); 
$content = fread ($fp,filesize ("temp.html")); 
$onepage = '20'; 
$sql = "select id from article where channel='$channelid'"; 
$query = mysql_query ($sql); 
$num = mysql_num_rows ($query); 
$allpages = ceil ($num / $onepage); 
for ($i = 0;$i<$allpages; $i++) 
{ 
if ($i == 0) 
{ 
$indexpath = "index.html"; 
} 
else 
{ 
$indexpath = "index_".$i."html"; 
} 
$start = $i * $onepage; 
$list = ''; 
$sql_for_page = "select name,filename,title from article where channel='$channelid' limit $start,$onepage"; 
$query_for_page = mysql_query ($sql_for_page); 
while ($result = $query_for_page) 
{ 
$list .= '<a href='.$root.$result['filename'].' target=_blank>'.$title.'</a><br>'; 
} 
$content = str_replace("{articletable}",$list,$content); 
if (is_file ($indexpath)) 
{ 
@unlink ($indexpath); //If the file already exists, delete it
} 
$handle = fopen ($indexpath,"w"); //Open the file pointer and create the file
 
if (!is_writable ($indexpath)) 
{ 
echo " File: ".$indexpath." Unable to write, please check its properties and try again! "; //Modified to echo
} 
if (!fwrite ($handle,$content)) 
{//Writes the information to a file
echo " Generate the file ".$indexpath." Failure! "; //Modified to echo
} 
fclose ($handle); //Close the pointer
} 
fclose ($fp); 
die (" The generation of paging file is completed, if the generation is not complete, please check the file permissions system to rebuild! "); 
?> 

Third: the smarty template generates static pages
Smarty has its own fetch function, which is similar to fread (), which can be used to generate static pages.
This example will look familiar to you. Yes, the example of fetch in the smarty manual is more classic than the official example.
The PHP code
 
<?php 
include("Smarty.class.php"); 
$smarty = new Smarty; 
$smarty->caching = true; 
// only do db calls if cache doesn't exist 
if(!$smarty->is_cached("index.tpl")) 
{// dummy up some data 
$address = "245 N 50th"; 
$db_data = array("City" => "Lincoln", "State" => "Nebraska", "Zip" => "68502"); 
$smarty->assign("Name","Fred"); 
$smarty->assign("Address",$address); 
$smarty->assign($db_data); 
}// capture the output 
$output = $smarty->fetch("index.tpl"); 
// This place is the key // do something with $output here 
echo $output; //Did hoho see the output and then what? Fwrite, and we get what we want.
$fp = fopen("archives/2005/05/19/0001.html", "w"); 
fwrite($fp, $content); 
fclose($fp); 
?> 

The PHP code
 
<?php 
ob_start(); 
echo "Hello World!"; 
$content = ob_get_contents();//Gets the entire output of the PHP page
$fp = fopen("archives/2005/05/19/0001.html", "w"); 
fwrite($fp, $content); 
fclose($fp); 
?> 

Related articles: