Generate php static (html) page program code _ PHP Tutorial

Source: Internet
Author: User
Generate static (html) page program code from php. Static pages are usually generated by dynamic pages to generate html pages. this reduces server load and is also a common optimization method for various websites, next I will share a static page generated by php, which generally generates html pages from dynamic pages. this reduces server load and is also a common optimization method for various websites, next I will share a static (html) page class generated by php.
The code is as follows:

Class create_html {

Private $ template;

// Template

Private $ file_name;

// File name

Private $ array;

// Data array

Function _ construct ($ file_name, $ template, $ array ){

// Construct a class

$ This-> template = $ this-> read_file ($ template, "r ");

// Read the template file

$ This-> file_name = $ file_name;

$ This-> array = $ array;

// Data

$ This-> html ();

// Generate html

}

Function html (){

// Generate html

While (ereg ("{([0-9] +)}", $ this-> template, $ regs )){

// {1 }.....

$ Num = $ regs [1];

// Obtain the sequence 1, 2, and 3.

$ This-> template = ereg_replace ("{". $ num. "}", $ this-> array [$ num], $ this-> template );

// Replace the data with html content

$ This-> write_file ($ this-> file_name, $ this-> template, "w + ");

// Generate an HTML file

}

}

Function read_file ($ file_url, $ method = "r "){

// Read the file

$ Fp = @ fopen ($ file_url, $ method );

// Open the file

$ File_data = fread ($ fp, filesize ($ file_url ));

// Read file information

Return $ file_data;

}

Function write_file ($ file_url, $ data, $ method ){

// Write a file

$ Fp = @ fopen ($ file_url, $ method );

// Open the file

@ Flock ($ fp, LOCK_EX );

// Lock the file

$ File_data = fwrite ($ fp, $ data );

// Write a file

Fclose ($ fp );

// Close the file

Return $ file_data;

}

}

# Example --------

# Read the email reply template ----------------------------

$ Title = "title ";

$ Navigation = "browser ";

$ Happy_origin = "author ";

$ Name = "test2.htm ";

$ Template = "default_tmp.php ";

// Replace {1} {2} in the template

$ Daytype = array (1 => $ title,

2 => $ navigation,

3 => $ happy_origin );

$ Htm = new Restore_email ($ template, $ daytype );

Echo $ htm-> pint ();

?>


...

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.