First, the principle. A camel looked at so much information, found that no matter what method, the principle is the same. is to use the program to read the corresponding data to replace the variables in the template, and then generate a static page. The main use of PHP is to use Fread () and Fwirte (). The static page generated after, will be involved in the problem of modification. Here you can use a regular matching method to replace the changed parts of the template. But this method is too troublesome, Camel camel recommended method is directly to the original version of the template cut off, rebuilt, oh, real it.
It is also important to note that this method of generating static pages is generally used for pages where changes are not frequent, such as the final page of information. For list pages, it is also desirable if information updates are not frequent. Now the online popular many can generate static page blog or forum program, are manually click the background "Generate HTML page" button to "semi-automatic" generation of HTML. And for some of the information is very large portal sites, it will not work. Because static pages are called "static" because they are not automatically changed. If the list of information is updated 100 times a day, the static list page will be regenerated 100 times. If I had 10 of these columns, it would be enough to spit blood.
Well, gossip less, now look at the actual program demo:
A: It's a bit of an OB function, the code is simpler and the efficiency is relatively high. A camel from a
High point of the source code, made a number of changes
<?php Ob_start ();
@readfile ("http://localhost/?package=pricab&place_port=4");
$text = Ob_get_flush ();
$myfile = fopen ("myfile.html", "w");
$text = Str_replace ("{counent}", $string, $text);
Fwrite ($myfile, $text);
Ob_clean ();
?>
Because even if you want to generate static pages, dynamic read that part is to be retained, the data into the database, the URL passed to the ReadFile function, and then read into the cache, fwrite can generate static page, this is the camel camel most appreciate a practice. The least number of lines of code, the most efficient. Camel camel This side requires http://localhost/?package=pricab&place_port=4 is a bare page, that is, simple content, no head, tail, menu. In order to be more free to customize their own template myfile.html. If the only requirement is to generate a static page,
Ob_start ();
@readfile ("http://localhost/?package=pricab&place_port=4");
$string = Ob_get_flush ();
$myfile = fopen ("myfile.html", "w");
Fwrite ($myfile, $string);
Ob_clean ();
We can over.
Second: normal generation of static HTML pages.
This is done step-by-step, fread in the page, and then Str_replace replace
The first is to create the final content page:
$title = "http://siyizhu.com Test template";
$file = "Twomax Inter test Templet,<br>author:matrix@two_ Max ";
$fp = fopen ("temp.html", "R");
$content = fread ($fp, FileSize ("temp.html"));
$cont ent = Str_replace ("{file}", $file, $content);
$content = Str_replace ("{title}", $title, $content);
$filename = "test/test.html";
$handle = fopen ($filename, "w");//Open file pointer, create file & nbsp /* Check whether the file is created and writable */ if (!is_writable ($filename))
{ die ("File:". $ filename. " Not writable, please check its properties and try again! "); }
if (!fwrite ($handle, $content)) {//write information to file die (" Generate file ". $ filename. " Failed! "); }
fclose ($handle); Turn off the pointer die (create file. $filename. " Success! ");
This step is relatively simple. Just a simple variable to replace it. If you want to generate a static list page, the principle is the same, use the program to generate a list of articles, it as a large variable, replace the template in the variable, the page of the list is so. Of course, if there is information update, the page of the list will also be regenerated.
<?php
$title = "http://";
$file = "Twomax Inter test Templet,<br>author:matrix@two_max";
$fp = fopen ("temp.html", "R");
$content = Fread ($fp, FileSize ("temp.html"));
$content = Str_replace ("{file}", $file, $content);
$content = Str_replace ("{title}", $title, $content);
Build 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 Build list
Echo $content;
$filename = "test/test.html";
$handle = fopen ($filename, "w"); Open file pointer, create file
/* Check whether the file is created and writable/if (!is_writable ($filename)) {die ("file:". $filename. " Not writable, please check its properties and try again! "); } if (!fwrite ($handle, $content)) {//write information to file die ("Generate file". $filename. " Failed! "); } fclose ($handle); Turn off the pointer die (create file. $filename. " Success! ");?>
About paging:
If we specify pagination, each page is 20 pieces. A channel list of the article through the database query for 45, then, first of all, we get the following parameters through the query: 1, the total number of pages, 2, per page. The second step, for ($i = 0; $i < allpages $i + +), page element acquisition, analysis, and article generation, are executed in this loop. The difference is that die ("Create file". $filename. " Success! This sentence is removed and put into the loop after the display, because the statement will abort the program execution. Cases:
<?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} $handle = fopen ($indexpath, "w"); Open the file pointer, create File/* Check whether the file is created and writable/if (!is_writable ($indexpath)) {echo "file:. $indexpath." Not writable, please check its properties and try again! "; Modify to Echo} if (!fwrite ($handle, $content)) {//write information to file echo "Generate file". $indexpath. " Failed! "; Modified to echo} fclose ($handle); Close pointer}
Fclose ($FP); Die ("The Raw content page file completes, if the build is not complete, please check the file permission system to regenerate!" ");? >
Third:smarty templates generate static pages
Camel camel is used in the Smarty template, Smarty has a fetch function, its function is somewhat similar to fread () can be used to generate static pages.
This example everyone must look familiar, yes, Smarty Manual on the Fetch function example, HoHo a camel borrow, than the official example is always very classic!
<?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; HoHo
See the results of the output?
And then what? fwrite, we get the results we want.
$fp = fopen ("archives/2005/05/19/0001.html", "w");
Fwrite ($fp, $content); fclose ($FP);
?>
<?phpob_start (); echo "Hello world!";
$content = ob_get_contents ()//Get all content of PHP page output
$fp = fopen ("archives/2005/05/19/0001.html", "w");
Fwrite ($fp, $content);
Fclose ($FP);
?>
PHP generates static page classes
/*********************/
/* */
* version:5.2.5 * *
* author:liqiangwork#sohu.com * *
* qq:570937581 * *
/* */
/*********************/
-----------------------------generate a static class-------------------------------
Class makehtml{
Public $MbUrl, $OUTURL, $AllHtml, $SouChar, $ObjChar; Variable
Public $row; Cursor
Public $Shuzusou, $Shuzuobj; Array of substituted strings
-----------------------initialization-------------------------
function __construct () {//initialization
$this->mburl= "";
$this->outurl= "";
$this->allhtml= "";
$this->sql= "";
$this->souchar= "";
$this->objchar= "";
}
-----------------------------automatically replace by field---------------------------
function Autoreplace () {
------------------automatically gets the string to replace-------------------
$tlen =count ($row);
$shuzu 1=array ();
$shuzu 2=array ();
if ($row) {
$i = 0;
foreach ($row as $key => $value) {
$shuzu 2= "<=\$". $key. " \$> ";
$shuzu 1= $value;
$i + +;
}
$this->replacehtml (SHUZU2,SHUZU1);
}
------------------automatically gets the string to replace-------------------
}
-----------------------------automatically replaced by field------------------------
-----------------------------Bulk Substitution Array--------------------------
function replacehtml ($Shuzusou, $Shuzuobj) {//Bulk substitution array
if (count ($Shuzusou)!=count ($Shuzuobj)) {
Exit ("Replace array mismatch");
}
if ($this->allhtml== "") {
Exit ("No content to replace");
}
for ($i =0; $i <count ($Shuzusou); $i + +) {
$this->allhtml=str_replace ($Shuzusou [$i], $Shuzuobj [$i], $this->allhtml);
Print ("<br>". $Shuzusou (i). " = ". $Shuzuobj (i)." <br> ")
}
}
-----------------------------The batch substitution array is completed--------------------------
-----------------------------Read the file---------------------------------
function Readfile () {
$file =fopen ($this->mburl, "R");
$fsize =filesize ($this->mburl);
$this->allhtml=fread ($file, $fsize);
Fclose ($file);
}
-----------------------------Read the file is complete------------------------------
-----------------------------Save the file---------------------------------
function SaveFile () {
$file =fopen ($this->outurl, "w");
Fwrite ($file, $this->allhtml);
Fclose ($file);
}
-----------------------------Save file is complete------------------------------
}
------------------------------generate a static class complete-------------------------------
The following are the referenced contents:
------------------Static Generation----
$MyMake =new makehtml;
$MyMake->mburl= "news_show.shtml";
$MyMake->readfile ();
$THTML = $MyMake->allhtml;
$shuzu 1=array ();
$shuzu 2=array ();
$shuzu 1[0]= "<=\ $keybord \$>";
$shuzu 1[1]= "<=\ $description \$>";
$shuzu 1[2]= "<=\ $title \$>";
$shuzu 1[3]= "<=\ $Title 1\$>";
$shuzu 1[4]= "<\$=Bid\$>";
$shuzu 1[5]= "<\$=Id\$>";
$shuzu 1[6]= "<=\ $Contentb \$>";
$shuzu 1[7]= "<\$=BigId\$>";
$shuzu 1[8]= "<=\ $Date \$>";
$shuzu 1[9]= "<=\ $City \$>";
$shuzu 1[10]= "<=\ $SmallId \$>";
$shuzu 1[11]= "<=\ $CityId \$>";
$shuzu 1[12]= "width=\" 100%\ "";
$MyMake->outurl= "news_show_1.shtml";
$shuzu 2[0]= "Array 0";
$shuzu 2[1]= "Array 1";
$shuzu 2[2]= "Array 2";
$shuzu 2[3]= "Array 3";
$shuzu 2[4]= "Array 4";
$shuzu 2[5]= "Array 5";
$shuzu 2[6]= "Array 6";
$shuzu 2[7]= "Array 7";
$shuzu 2[8]= "Array 8";
$shuzu 2[9]= "Array 9";
$shuzu 2[10]= "Array 10";
$shuzu 2[11]= "Array 11";
$shuzu 2[12]= "width=\" 95%\ "";
$MyMake->replacehtml ($shuzu 1, $shuzu 2);
$MyMake->savefile ();
------------------Static build Completed-----------
This article from the PHP Forum article address: http://bbs.php.cn/thread-36818-1-1.html