PHP dynamic page Generation static Page 3 common methods _php tips

Source: Internet
Author: User
Tags fread rand

The pages that generate static pages are very simple to define templates and template headers, and then replace them with Str_replace, which is the most common method, the other is to use ob_get_contents output to generate HTML, and one of the less recommended is to use File_get _contents direct execution of access to remote files and then save, very poor performance.

The specific method is briefly described as follows:
1. Use the file function to get the template string for the static page, and then replace it with the Str_replace function and write to the new file.
2. Use the output control function of PHP to get the static page string, then write to the new file.

Copy Code code as follows:
$filemodel = "template/it.php"; Template Address
$file =fopen ($filemodel, "RB"); Open template, get file pointer
$temp =fread ($file, FileSize ($filemodel)); Get template file HTML code

Method One: Ob_get_contents ()

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

Copy Code code as follows:
Ob_strart ()//Open buffer
$fn =date (' Ymdhis '). Rand (1000,9999). HTML ';//Generate file name
Require ("supply.php");/load the file to generate a static page, because the background has ob_clean () so it won't show up
$fs =fopen ($FN, ' w ');//Open static page file
Fwrite ($fs, ob_get_contents ());//Generate Static file
Ob_clean ()//Empty cache

Method Two: file_get_contents ();

Copy Code code as follows:
$fn =date (' Ymdhis '). Rand (1000,9999). HTML ';
$url = ' http://'. $_server[' Http_host ']. " /";//Note
$content =file_get_contents ($url);
$fs =fopen ($FN, ' w ');
Fwrite ($fs, $content);

The following is an explanation of the above note, if in some you are using only the filename, not the URL then if you have a file in this document using reference files such as require (' header.php '); Then the contents of the header.php will not be displayed.

Method Three: Str_replace ()

Copy Code code as follows:
$filemodel = "supply.php"; String 5$file=fopen ($filemodel, "w+");
$temp =fread ($file, FileSize ($filemodel));
$temp =str_replace ("[title]", $title, $temp);
$temp =str_replace ("[Posttime]", $postTime, $temp);
$temp =str_replace ("[Content]", $content, $temp);

This method is suitable for very simple pages, if there is a reference file such as require (' header.php ') in the supply.php, then the content in header.php will not be displayed
In practical applications, you can write a class that generates a static page,
Copy Code code as follows:
/*+++
|
| How to use
| $shtml = new sHTML ($URL, $FileBag, $FolderName, $fileid)
| $URL: Page URL Address
| $FileBag: Folder Tag 1 is: Specify folder
| 2 is: Default folder (Time (month))
| Storing paths $FolderRoot HTML files
| $FolderName the name of the specified folder can be written as empty ("") $FileBag 2;
| $fileid static page name (suffix defaults to. html)
|
|
|
/*++*/
Class Shtml
{
var $message 1= "Error 1:you Write class Shtml is wrong!" The second parameter is 1 or 2 in this class!. ";
var $message 2= "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)
{
Ob_start ();
Require_once $this->url;
Shtml::writehtml ($Folder, ob_get_contents ());
Ob_clean ();
}
/********** the specified folder *****************/
Public Function Usefolder ()
{
if ($this->filebag==1)
{
$Folder = $this->filename;
}
else if ($this->filebag==2)
{
$Folder =date (' Ymd ', Time ());
}
Else
{
Exit ($this->message1);
}
if (!is_dir ($this->fileroot. $Folder)) {mkdir ($this->fileroot. $Folder, 0700);}
Shtml::loadcontent ($Folder);
}
/********** Generate static page *****************/
Public Function writehtml ($Folder, $cache _value)
{
$file = fopen ($this->fileroot. $Folder. /'. $this->fileid. html ', ' w+ ');
Fwrite ($file, $cache _value);
Fclose ($file);
}
}
$fileid = 2;
$shtml = new sHTML ("Http://www.jb51.net", 1, "" "," CC ", $fileid);

Summary, this generated HTML program code has no raw content page, if a lot of articles it will only generate an article, if you want to improve we need to make a relatively large change, this is not introduced. Interested friends can test the improvement, I believe there will be no small harvest!

I hope this article will help you with your PHP program design.

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.