PHP static html file generation methods

Source: Internet
Author: User
PHP generates static html files. Php is used to generate static files. most of the files are generated by replacing them with templates. for example, if I define {A} as A tag in php, I only need to use fopen to read the template file, then, repla uses php to generate static files. The most commonly used static files are generated in the form of templates. for example, if I define {A} as A tag in php, I only need to use fopen to read the template file, replace {A} with replace to generate A static html file instance for PHP.

1. The following is a template method!

The code is as follows:

$ Fp = fopen ("templets.html", "");
If ($ fp ){
$ Fup = fread ($ fp, filesize ("templets.html "));
$ Fp2 = fopen ("html.shtml", "w ");
If ($ fwrite ($ fp2, $ fup )){
$ Fclose ($ fp );
$ Fcolse ($ fp2 );
Die ("template written successfully ");
} Else {
Fclose ($ fp );
Die ("failed to write template! ");
}
}
?>

Simply write the template into a file and save it as HTML.


2. generate html file names by time

The code is as follows:
$ Content = "this is a test file for generating a webpage statically based on the date and time file name,
The file name format is generally year, month, day, hour, minute, second. html ";
$ Date = date ('ymdhis ');
$ Fp = fopen (date ('ymdhis '). '.html', "w ");
// This function can be used to open the local or remote file 'W,
The file pointer points to the beginning and sets the length of the original file to 0. If the file does not exist,
Create a new file.
If (fwrite ($ fp, $ content )){
// The format is. int fwrite (int fp (file name), string (content ),
Int [length] (length); this function writes the string to the fp pointer of the file data stream.
If the length is specified, the specified length string is written or the end of the string is written.
Fclose ($ fp); // The function is used to close the fp pointer of an opened file.
True is returned for success, and false is returned for failure.
Die ("template written successfully ");
} Else {
Fclose ($ fp );
Die ("failed to write template! ");
}
Echo ($ content );
?>

3. The following is a method for converting file names.

The code is as follows:

$ S_fname = "93e. php ";
$ O_fname = "93e.htm ";
Ob_end_clean ();
Ob_start ();
Include ($ s_fname );
$ Length = ob_get_length ();
$ Buffer = ob_get_contents ();
$ Buffer = eregi_replace ("r", "", $ buffer );
Ob_end_clean ();

$ Fp = fopen ($ o_fname, "w + ");
Fwrite ($ fp, $ buffer );
Fclose ($ fp );
?>

In this way, 93e. php can be converted to a static HTML file. Note that the files to be converted cannot contain ob_end_clean (); and ob_start (); statements, and the directory must have write permission.

The above three methods generate html pages and update them every time. below we can use dynamic page cache technology to instance html + php pages. this method is better than above.

First set. htaccess file, convert Dynamic Call parameters to static html url address, for example, the file under the post Directory, forward to the wp-post.php file in the root directory, the added statement is similar:

The code is as follows:

RewriteRule ^ post/(%a-z0-9-%}.html) $ wp-post.php? $1 $2

Then modify the wp-post.php file and add the following PHP code at the beginning of the file:

The code is as follows:

Ob_start ();
$ Qstring = isset ($ _ SERVER ["QUERY_STRING"])? $ _ SERVER ["QUERY_STRING"]: "";
Define ("HTML_FILE", $ _ SERVER ['document _ root']. "/post/". $ qstring );

If (file_exists (HTML_FILE ))
{
$ Lcft = filemtime (HTML_FILE );
If ($ lcft + 3600)> time () // checks whether the last HTML file was generated for more than 1 hour. if not, the file content is output directly.
{
Echo (file_get_contents (HTML_FILE ));
Exit (0 );
}
}

Then there is the existing PHP code, and the following PHP code is added at the end of the current code:

The code is as follows:

Define ("HTMLMETA "," ");
$ Buffer = ob_get_flush ();
$ Fp = fopen (HTML_FILE, "w ");
If ($ fp)
{
Fwrite ($ fp, $ buffer. HTMLMETA );
Fclose ($ fp );
}

Now, check your static HTML page. if a comment line appears at the end of the page, it means that a static HTML file has been created successfully.

...

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.