-
- ob_start();
- @readfile("http://bbs.it-home.org/");
- $text = ob_get_flush();
- $myfile = fopen("myfile .html","w");
- $text =
- str_replace ("{counent}",$string,$text);
- fwrite($myfile,$text);
- ob_clean();
- ?>
Copy the code
Because even if you want to generate a static page, the dynamic reading part must be retained. After inserting the data into the database, pass the url to the readfile function, then read it into the cache, and fwrite to generate the static page. This This is a practice that Tutu appreciates most. The fewest lines of code and the highest efficiency. http://bbs.it-home.org/ is a naked page, that is, pure content, without header, tail, or menu. In this way, you can customize your own template myfile.html more freely. If you only require the generation of static pages, this basically meets the needs.
2. Normally generate static html page
fread the incoming page and then replace it with 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 the 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 the file
- die ("Generate file".$ filename."Failed!");
- }
- fclose ($handle); //Close the pointer
- die ("Create file".$filename."Success!");
- ?>
Copy code
This step is just a simple variable replacement.
If you want to generate a static list page, the principle is the same. Use a program to generate the article list, treat it as a large variable, and replace the variables in the template. This is how the page turning page of the list is.
Of course, if the information is updated, the list page will also need to be regenerated.
-
- $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);
- //Start generating 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 );//End of generating list
- // echo $content;
- $filename = "test/test.html";
- $handle = fopen ($filename,"w");
- //Open the file pointer and create the file
- /* Check whether the file is created and writable*/
- if(!is_writable ($filename))
- {
- die ("File: ".$filename." is not writable, please check its properties and try again!");
- }
- if(!fwrite($handle,$content))
- { //Write information to the file
- die ("Generate file".$filename."Failed!");
- }
- fclose($handle); //Close the pointer
- die ("Create file".$filename."Success!");
- ?>
Copy code
About turning pages:
If we specify paging, there will be 20 articles per page. There are 45 articles in a certain sub-channel list according to the database query. First, we obtain the following parameters through query: 1, the total number of pages; 2, the number of articles per page. The second step, for ($i = 0; $i
-
- $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 .= ''.$title.'
';
- }
- $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
- /*Check whether the file is created and writable*/
- if (!is_writable ($indexpath))
- {
- echo "File: ".$indexpath ."Not writable, please check its properties and try again!"; //Change to echo
- }
- if (!fwrite ($handle,$content))
- {//Write information to the file
- echo "Generate file" .$indexpath."Failed! "; //Modify to echo
- }
- fclose ($handle); //Close pointer
- }
- fclose ($fp);
- die ("Generating the paging file is completed. If the generation is incomplete, please check the file permission system and try again. Generate! ");
- ?>
Copy the code
3. Smarty template generates static pages
smarty has its own fetch function, which is similar to fread() and can be used to generate static pages.
Regarding smarty technology, you can read a few articles here:
1) Basic settings of smarty
2) Applications related to smarty caching
3) How to generate static pages using smarty
4) Detailed introduction to php template engine Smarty
-
- 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 Have you seen the result of the output? Then what? After fwrite, we will get the result we want.
- $fp = fopen("archives/2013/05/19/0001.html", "w");
- fwrite($fp, $content);
- fclose($fp);
- ?>
Copy Code
Copy code The code is as follows:
-
- ob_start();
- echo "Hello World!";
- $content = ob_get_contents();//Get all the contents output by the php page
- $fp = fopen("archives/ 2013/05/19/0001.html", "w");
- fwrite($fp, $content);
- fclose($fp);
- ?>
-
Copy code
P.S.:
Blog or forum programs that can generate static pages will "semi-automatically" generate HTML by manually clicking the "Generate HTML Page" button in the background.
Articles you may be interested in:
Example of php generating static page function (php2html)
How to generate static pages in php (three functions)
Reference on how to generate html static pages with php
A class written in php to generate static pages
Code to generate html static pages from all content in the database
How to automatically generate static pages on a virtual host at regular intervals
Detailed tutorial on generating static pages with php
Solution to the problem that pseudo-static pages cannot be accessed in apache
Code written by php about spider crawling records of static pages
How to generate static pages using smarty
How to generate static pages with PHP
Solution to the problem that apache cannot access pseudo-static pages
|