Php static page generation method

Source: Internet
Author: User
First, let's talk about the principle. A certain camel checked so much information and found that no matter what method is used, the principle is the same. It is to use the program to read the corresponding data to replace the variables in the template, and then generate a static page. Fread () and fwirte () are used in php (). First, let's talk about the principle. A certain camel checked so much information and found that no matter what method is used, the principle is the same. It is to use the program to read the corresponding data to replace the variables in the template, and then generate a static page. Fread () and fwirte () are used in php (). After a static page is generated, the modification will be involved. Here we can use the regular expression matching method to replace the changed parts in the template. However, this method is too troublesome. the recommended method is to cut down and regenerate the original template.

It should also be noted that this method of generating static pages is generally used for pages that are not frequently changed, such as the final page of information. For list pages, it is also advisable to update information frequently. Currently, many blogs or forum programs that can generate static pages are popular on the internet. they are "semi-automatic" html generation by manually clicking the "generate html page" button in the background. However, portal websites with a large amount of information do not work. Static pages are called static pages because they cannot be changed automatically. If the information list is updated 100 times a day, the static list page will be re-generated 100 times. If I have 10 such columns, I think it's enough to vomit blood.

Now let's take a look at the actual program demonstration:

First: it is a hacker using ob functions. the code is relatively simple and the efficiency is relatively high. From a certain

The source code obtained by Gao Ren has made some changes.

@ 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 a static page, dynamic reading of that part should also be retained. after the data is inserted into the database, the url is passed to the readfile function and then read into the cache, fwrite can generate static pages at a moment. this is a kind of method most appreciated by camels. The minimum number of lines of code is the most efficient. Http: // localhost /? Package = pricab & place_port = 4 is a raw page with no header, tail, or menu. In this way, we can compare the free template myfile.html. If you only want to generate static pages,
Ob_start ();
@ Readfile ("http: // localhost /? Package = pricab & place_port = 4 ");
$ String = ob_get_flush ();
$ Myfile = fopen ("myfile.html", "w ");
Fwrite ($ myfile, $ string );
Ob_clean ();

You can overwrite it.

Second: generate a static html page.

This is done step by step. fread comes into the page and then replaces str_replace

First, create the final content page:

$ Title = "http://siyizhu.com test template ";
$ File = "TwoMax Inter test templet,
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 a file/* check whether the file is created and writable */if (! Is_writable ($ filename ))
{Die ("File:". $ filename. "cannot be written. check its attributes and try again! ");}
If (! Fwrite ($ handle, $ content) {// write information to the file die ("generate file". $ filename. "failed! ");}
Fclose ($ handle); // Close the die pointer ("create file". $ filename. "successful! ");

This step is relatively simple. Just replace the variables. If you want to generate a static list page, the principle is the same. you can use a program to generate a list of articles and use it as a large variable to replace the variables in the template. this is the case when you flip the list. Of course, if there is any information update, the list will be re-generated by turning pages.

$ Title = "http ://";
$ File = "TwoMax Inter test templet,
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 );
// Starts generating the list
$ List = '';
$ SQL = "select id, title, filename from article ";
$ Query = mysql_query ($ SQL );
While ($ result = mysql_fetch_array ($ query )){
$ List. = ''. $ result ['title'].'
';}
$ Content. = str_replace ("{articletable}", $ list, $ content); // The Generation list ends.
// Echo $ content;
$ Filename = "test/test.html ";
$ Handle = fopen ($ filename, "w"); // open the file pointer and create a file
/* Check whether the file is created and writable */if (! Is_writable ($ filename) {die ("File:". $ filename. "cannot be written. check its attributes and try again! ");} If (! Fwrite ($ handle, $ content) {// write information to the file die ("generate file". $ filename. "failed! ") ;}Fclose ($ handle); // Close the die pointer (" create file ". $ filename." successful! ");?>

Page turning:

For example, we specify 20 pages per page. If the number of articles in a subchannel list is 45 in the database, we first obtain the following parameters through the query: 1, the total number of pages; 2, the number of entries per page. Step 2: for ($ I = 0; $ I <allpages; $ I ++), page element acquisition, analysis, and article generation are all executed in this loop. The difference is that die ("create file". $ filename. "succeeded! "; This sentence is removed and displayed after the loop, because the statement will stop the program execution. Example:

$ 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. = ''. $ title .'
';}
$ Content = str_replace ("{articletable}", $ list, $ content );
If (is_file ($ indexpath) {@ unlink ($ indexpath); // delete if the file already exists} $ handle = fopen ($ indexpath, "w "); // open the file pointer and create a file/* check whether the file is created and writable */if (! Is_writable ($ indexpath) {echo "File:". $ indexpath. "cannot be written. check its attributes and try again! "; // Modify to echo} if (! Fwrite ($ handle, $ content) {// write information to the file echo "generate file". $ indexpath. "failed! "; // Modify to echo} fclose ($ handle); // Close the pointer}
Fclose ($ fp); die ("Generation of paging files is complete. if generation is incomplete, check the file permission system and generate a new one! ");?>
Third: static page generated by the smarty Template
Camels use smarty Templates. smarty has its own fetch function, which is a bit similar to fread () that can be used to generate static pages.
This example seems familiar to everyone. right, the example of the fetch function in the smarty manual, which hoho has borrowed is always a classic example!
$ Smarty = new Smarty;
$ Smarty-> caching = true;
// Only do db CILS 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" = & gt; "68502"
);
$ Smarty-> assign ("Name", "Fred ");
$ Smarty-> assign ("Address", $ address );
$ Smarty-> assign ($ db_data );}
// Capture the output $ output = $ smarty-> fetch ("index. tpl ");
// This is the key. // do something with $ output here echo $ output; // hoho
See the output result.
What then? Fwrite, and we will get the result we want.
$ Fp = fopen ("archives/2005/05/19/0001 .html", "w ");
Fwrite ($ fp, $ content); fclose ($ fp );
?>
$ Content = ob_get_contents (); // retrieves all the content output on the php page.
$ 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; // string array to be replaced
// ----------------------- Initialize -------------------------
Function _ construct () {// Initialization
$ This-> MbUrl = "";
$ This-> OutUrl = "";
$ This-> AllHtml = "";
$ This-> SQL = "";
$ This-> SouChar = "";
$ This-> ObjChar = "";
}


// ------------------------------- Automatically replace --------------------------- By field ---------------------------
Function AutoReplace (){
// ------------------ Automatically obtain the string to be replaced -------------------
$ Tlen = count ($ row );
$ Shuzu1 = array ();
$ Shuzu2 = array ();
If ($ row ){
$ I = 0;
Foreach ($ row as $ key => $ value ){
$ Shuzu2 = "<= $". $ key. "\ $> ";
$ Shuzu1 = $ value;
$ I ++;
}
$ This-> Replacehtml (shuzu2, shuzu1 );
}
// ------------------ Automatically obtain the string to be replaced -------------------
}
// ------------------------------- Automatically complete field replacement ------------------------


// ------------------------------- Replace arrays in batches --------------------------
Function Replacehtml ($ Shuzusou, $ Shuzuobj) {// replace arrays in batches
If (count ($ Shuzusou )! = Count ($ Shuzuobj )){
Exit ("replacement array does not match ");
}
If ($ this-> AllHtml = ""){
Exit ("no content to be replaced ");
}
For ($ I = 0; $ I $ This-> AllHtml = str_replace ($ Shuzusou [$ I], $ Shuzuobj [$ I], $ this-> AllHtml );
// Print ("
". $ Shuzusou (I)." = ". $ Shuzuobj (I )."
")
}
}
// ------------------------------- Replace arrays in batches --------------------------

// ----------------------------- Read the file ---------------------------------
Function Readfile (){
$ File = fopen ($ this-> MbUrl, "r ");
$ Fsize = filesize ($ this-> MbUrl );
$ This-> AllHtml = fread ($ file, $ fsize );
Fclose ($ file );
}
// ------------------------------- File reading is completed ------------------------------
// ----------------------------- Save the file ---------------------------------
Function SaveFile (){
$ File = fopen ($ this-> OutUrl, "w ");
Fwrite ($ file, $ this-> AllHtml );
Fclose ($ file );
}
// ------------------------------- Save the file ------------------------------
}
// ------------------------------ Complete generating static classes -------------------------------

Reference content is as follows:
// ------------------ Static generation ----
$ MyMake = new Makehtml;
$ MyMake-> MbUrl = "News_Show.shtml ";
$ MyMake-> Readfile ();
$ THTml = $ MyMake-> AllHtml;
$ Shuzu1 = array ();
$ Shuzu2 = array ();
$ Shuzu1 [0] = "<=\$ keybord \ $> ";
$ Shuzu1 [1] = "<=\$ description \ $> ";
$ Shuzu1 [2] = "<=\$ title \ $> ";
$ Shuzu1 [3] = "<=\$ Title1 \ $> ";
$ Shuzu1 [4] = "<\$ = Bid \ $> ";
$ Shuzu1 [5] = "<\$ = Id \$> ";
$ Shuzu1 [6] = "<=\$ Contentb \ $> ";
$ Shuzu1 [7] = "<\$ = BigId \ $> ";
$ Shuzu1 [8] = "<=\$ Date \ $> ";
$ Shuzu1 [9] = "<=\$ City \ $> ";
$ Shuzu1 [10] = "<=\$ SmallId \ $> ";
$ Shuzu1 [11] = "<=\$ CityId \ $> ";
$ Shuzu1 [12] = "width = \" 100% \"";
$ MyMake-> OutUrl = "News_show_1.shtml ";
$ Shuzu2 [0] = "array 0 ";
$ Shuzu2 [1] = "array 1 ";
$ Shuzu2 [2] = "array 2 ";
$ Shuzu2 [3] = "array 3 ";
$ Shuzu2 [4] = "array 4 ";
$ Shuzu2 [5] = "array 5 ";
$ Shuzu2 [6] = "array 6 ";
$ Shuzu2 [7] = "array 7 ";
$ Shuzu2 [8] = "array 8 ";
$ Shuzu2 [9] = "array 9 ";
$ Shuzu2 [10] = "array 10 ";
$ Shuzu2 [11] = "array 11 ";
$ Shuzu2 [12] = "width = \" 95% \"";
$ MyMake-> Replacehtml ($ shuzu1, $ shuzu2 );
$ MyMake-> SaveFile ();
// ------------------ Static generation completed -----------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.